How to use x:Uid in windows phone 8? - xaml

We can use x:Uid in windows-8 as
Where in .resw I have define
mainTitle.Text = "Your Name"
In this way TextBlock's text becomes Your Name.
How can I achieve the same in windows phone 8?
When I put mainTitle.Text in .resx it gives error.

You have to use binding in Windows Phone 8.
The most simple way to see this in action is to create a new project and take a look at MainPage.xaml. The binding is demonstrated in the following comment
For example:
Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}"
To localize some text, you bind the LocalizedStrings class (created with the project) which wraps your static Resource file.
<TextBlock Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}" />
The LocalizedStrings resource is already there in your App.xaml
<local:LocalizedStrings x:Key="LocalizedStrings"/>

You can use binding here
<TextBlock Text="{Binding Path=LocalizedResources.ApplicationTitle, Source={StaticResource LocalizedStrings}}"/>
For more about localization read How to build a localized app for Windows Phone
And to know about x:Uid read x:Uid Directive

Binding is one solution but simply you can do the following.
If you have the following AppBarButton
<AppBarButton
Name="AppBarButtonMore"
x:Uid="appbar_more_title"
Click="AppBarButtonMore_Click"/>
add the string file with the key appbar_more_title.Label to your resources and your button will take that string as label property.This is also valid for any UI controls.

Related

Can a Binding value be created as a XAML Page resource?

Hi for rapid UI testing of Binding data at design-time in a UWP app, I was wondering if there was a way to create Binding values as resources in XAML. So instead of having to create a model, instantiate that model at design-time in Page.Resources, I just wanted to create essentially some constants I could use as Bindings.
Here are some examples of where I’d like the test bindings to work:
<TextBlock Text="{Binding title, ConverterParameter=lower, Converter={StaticResource StringFormatConverter}, Mode=TwoWay}" />
<TextBlock Text="{Binding title}" />
Given that scenario is there a way I could say define title as a named key with a string value that would work above?
Note that I am using Binding and not x:Bind since I want to use design-time data and I don’t want to have to rely on using the FallbackValue parameter.
Thanks for any ideas!
Rick
You can't bind to a static resource directly, but you can bind to a property of a static resource instead.
You can create a class implementing INotifyPropertyChanged which would have properties like Title. You would then create an instance of this class and store it as resource:
Application.Current.Resources[ "Data" ] = new MyDataClass();
Now you could use it in binding like this:
<TextBlock Text="{Binding Title, Source={StaticResource Data}}" />

Reference string resource in Windows Phone 8.1 XAML

I am unclear how do I reference a control text property using string resources in Windows Phone 8.1?
For instance I have:
<HubSection x:Name="MySection" HeaderTemplate="{StaticResource MyHeaderTemplate}" Header="{StaticResource MyText}">
and
<AppBarButton Name="MyAppBar" Label="{StaticResource MyText}" ...>
and
They both reference MyText which I want to load from resource .resx file.
To Refer Any control Property from resources in the XAML, create a x:Uid for that control say for a TextBlock, I made a control like :
<TextBlock x:Uid="Header" Text="from Resx" Style="{ThemeResource TitleTextBlockStyle}"/>
And in the Resources.resx define a new Resource with name Header.Text and Value as desired say, My Header Text. See the screenshot below :

x:name of controls in ApplicationViewStates

I am working in windows store apps and I need help..
I am trying to adapt my app for the differents visual state (FullScreenLandscape, FullScreenPortrait, Snapped, etc) and I need to reuse the controls for each visual state..
I create a grid for each visual state for example
<Grid x:Name="PortraitView" Visibility="Collapsed">
</Grid>
<Grid x:Name="FillView" Visibility="Visible">
</Grid>
<Grid x:Name="SnapView" Visibility="Collapsed">
</Grid>
but my problem is that I can not repeat the x:name of the controls that I set in the Grid= PortraitView..
how can I resolve it?
thanks
From MSDN
The most common usage of this property is to specify a XAML element
name as an attribute in markup. This property essentially provides a
WPF framework-level convenience property to set the XAML x:Name
Directive.
Names must be unique within a namescope.
For more information, see XAML Namescopes.

Order of bindings in Windows Phone 8

I am facing this issue regarding data binding and binding in a converter while developing a Windows Phone 8 app.
I am trying to send the button to a converter so I can access all of its properties using the following code
<Button Content="{Binding OwnBoard, Mode=OneWay}" Grid.Row="0" Background="{Binding RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource BoardToBackConv}}" />
When I hit the break point in the converter I have the button as the binding object, but the Content property of the control is Null. I need to access the object stored in the Content property.
As far as I know, because of the order of the bindings, the Content property should already contain a value.
Any idea how to make this work?
I have managed to understand why what I want to do is not going to work.
Keeping it short, the converter is executed during the InitializeComponent() call, and at that time the other binding was not done, because there is not DataContext for the view yet.

How to reuse XAML code blocks without binding?

I have this XAML code block:
<StackPanel Orientation="Horizontal">
<TextBlock Text="Hello " />
<TextBlock Text="World" />
</StackPanel>
I would like to define this block (for example in /Common/CustomStyles.xaml) to include it on different XAML pages. How does it work?
I do not need a DataTemplate or something similar because the text is static.
You could put it in a user control and re-use it that way. If it's just the text you are interested in then you could use a resource file - this would update the textbox on any screen as long as it had the right uid.