UWP changing ResourceDictionary programmatically - xaml

I am designing an app that retrieves colors from a REST API and needs to use these to theme the app. I am trying to define the colors inside of styles in my ResourceDictionary since one of the colors I need to set is the PointerOver VisualState of a button. I am currently using the below method to change the SolidColorBrushes.
App.Current.Resources["PrimaryBrush"] = myNewSolidColorBrush;
When I bind my element's background directly to this StaticResurce the colors work perfectly. But, when I move this inside of a style into my separate file with a ResourceDictionary and bind the background inside the style to the to that SolidColorBrush resource that I am changing.
My problem is that when I change the SolidColorBrush programmatically, the binding in my style does not update! Is there a way to either force the resource dictionary to revaluate its bindings or programmatically change the setter values in my defined style?

My problem is that when I change the SolidColorBrush programmatically, the binding in my style does not update!
<ResourceDictionary>
<SolidColorBrush x:Key="PrimaryBrush" Color="Yellow"/>
</ResourceDictionary>
The problem is that you have assigned the new myNewSolidColorBrush object to PrimaryBrush, and the StaticResource doesn't support dynamic change. For you requirement you could modify the property of the PrimaryBrush object.
(App.Current.Resources["PrimaryBrush"]as SolidColorBrush).Color = Colors.Red;

Related

How to apply stroke to textblock in xaml under uwp 10 app

I want to apply a black stroke on a white textblock in uwp app like the one shown in the picture below
It's actually called outlined text. The easiest way is find a specific outline font and apply it for the FontFamily property of the TextBlock control.
For example, you could download the font file '.ttf' and put it in the 'Assets' folder.
<TextBlock FontSize="42" Text="Hello World" FontFamily="Assets/your outlined font.ttf#font name"/>
Another way is using Win2D relevant APIs to render outlined text.
You could refer to the Win2D-ExampleGallery's TextOutlines sample for more details.

UWP ToggleSwitch put Content on the Left

Is there an easy way to get the content (text) on the left side of a ToggleSwitch control? (Else than messing up the Template).
Thanks
You should alter the template if you want to do it correctly.
However, if you want to fix it in a hacky way, change the FlowDirection of the control. But then you'll have to tweak with margins and alignments to make it look decent when used with other controls (see image below for default alignment).
<StackPanel>
<ToggleSwitch OffContent="Test" OnContent="Test2"></ToggleSwitch>
<ToggleSwitch OffContent="Test" OnContent="Test2" FlowDirection="RightToLeft"></ToggleSwitch>
</StackPanel>
A modified template displaying SwitchToggle content on the left can be found here.

UWP mediaplayerelement how to display extra text on top part of transport media control?

I am writing a UWP 14393 app using mediaplayerelement in xaml file, I am wondering how can I display extra information in text on top of custom transport media control so that when player control is up, the text will show up at the same time?
For example, for a video player showing an online stream, and at the top left corner shows streamer name, view count, etc. The information only shows up when player control shows up.
Obviously, the best way to do this is to put the text inside custom transport media control, is it doable? If not, how can I achieve this?
I am a newbie at UWP, so any help will be welcome, thanks.
You can custom the MediaTransportControls's style, and add your own content to <Border x:Name="ControlPanel_ControlPanelVisibilityStates_Border">. See my test.
First download MediaTransportControls's style from my gist.
Or you can find it in your pc's generic.xaml file.
Then add this style to App.xaml.
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MediaTransportControls.xaml"/>
</ResourceDictionary.MergedDictionaries>
Then you can use your style in MediaTransportControls.
<MediaPlayerElement
Width="300" Height="500"
AreTransportControlsEnabled="True"
Source="ms-appx:///Assets/elephantsdream-clip-h264_sd-aac_eng-aac_spa-aac_eng_commentary-srt_eng-srt_por-srt_swe.mkv">
<MediaPlayerElement.TransportControls>
<MediaTransportControls Style="{StaticResource myMediaTransportControlsStyle}">
</MediaTransportControls>
</MediaPlayerElement.TransportControls>
</MediaPlayerElement>
<MediaPlayerElement AreTransportControlsEnabled="True">
<MediaPlayerElement.TransportControls>
<MediaTransportControls>
<Grid>
<...Put any content here like textblocks and buttons all this content will be part of controls so they will appear and disappear along with the controls, this Grid covers all the area on the screen ( above the controls bar )...>
</Grid>
</MediaTransportControls>
</MediaPlayerElement.TransportControls>
</MediaPlayerElement>

WP81 Create XAML style to target all pages in the app

I want to set the background image for all the pages in my windows phone 8.1 app (RT not Silverlight) by declaring a style that targets the page.
Like this:
<Style TargetType="Page">
<Setter Property="Background" Value="Red" />
</Style>
It seems to work in the designer, as I see the red background on all my pages. However when I actually run the app the background is missing (black, blank) not red.
Some of the pages in my app derive from a custom type (which derives from Page) and I know that TargetType doesn't inherit. So I added additional styles for these:
<Style TargetType="local:ViewBase">
<Setter Property="Background" Value="Red" />
</Style>
Again, in the designer I see the Red (tho strangely enough I also saw the red when I was only targeting Page). Upon launch again however, the background is not Red, but blank (black).
I could easily give it a key, or add the Background property to every page and bind it to a resource, but I thought the whole point of Implicit styles was to allow me to override every instance of the control...
Can I not target a Page for a default (implicit) style?
aha: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/bb927601-2e3e-45ac-afe8-621df57b9738/why-does-style-targettypepage-not-work-in-an-application?forum=winappswithcsharp
looks like this is a known issue, boo I say! I'll set the background to a resource then.
Why don't you override brush in application resource
<SolidColorBrush x:Name="ApplicationPageBackgroundThemeBrush" Color="RED"/>

Access a nested UserControl's ContentPresenter with Blend

I've created a simple UserControl in Blend which contains (amongst other things) a ContentPresenter.
I'd like to be able to drop this UserControl onto another UserControl and then add other controls into its ContentPresenter, but when I include it in the second UserControl I can't see a way to do this.
Using Blend, how do I expose the underlying ContentPresenter so that its contents can be set visually (ie: so they appear as child controls in the Objects and Timeline window)?
If it’s a content control, to add child controls you set them as the content of the control
<MyContentControl>
<Grid x:Name=”ImAChild”>
</Grid>
</MyContentControl>
Edit: now that I think about it you might need to do something to make the Content property the default property which is populated by the inner xaml of your control, I can't remember exactly what it was but if your xaml looks like this (below) it doesn't matter anyway
<MyContentControl>
<MyContentControl.Content>
<Grid x:Name=”ImAChild”>
</Grid>
</MyContentControl.Content>
</MyContentControl>
Edit2
Your MyUserControl would need a MyContent property of type object and it'd have to somehow display the value of that property. You might be able to create it in the setter of the MyContent property but it seems "hacky"
<MyUserControl>
<MyUserControl.MyContent>
<Grid x:Name=”ImAChild”>
</Grid>
</MyContentControl.MyContent>
</MyUserControl>