use resource file in XAML - c#

I tried to use Resources files (.resw) for multi-language.
On the project's root folder, I now have :
.\Strings\fr-FR\Resources.resw
&
.\Strings\en-US\Resources.resw
On both files, I just have
NAME VALUE
Welcome Welcome !
and So, I tried to call this reference into a XAML file, but the TextBlock stays empty
<TextBlock Text="" x:Uid="Welcome" MinWidth="200" Height="20" />
If the Text is set manualy, it appears, but here, nothing appears.
Any ideas ?

the name in the string of your resw-file should be Welcome.Text, because you are setting the text property of the textblock.
Then:
<TextBlock x:Uid="Welcome" MinWidth="200" Height="20" />

Related

How to set TextBlock text property to a string resource?

I've been following this tutorial Using String resources (xaml) to set up string resources, in a Windows Universal project. But even though I've set the Uid of the text block to the string resource name, the string contents aren't displayed in the text block during testing the app.
Does anyone know where I might be missing a step in setting up the string resource
as the text block text value?
This is the xaml definition for the text block, showing the Uid set to the same name as the name of the string in resources "About":
<TextBlock x:Uid="About"
Grid.Row="1"
Grid.RowSpan="2"
Width="400"
Height="300"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Text=""
TextWrapping="Wrap" />
This is the res file itself:
And this is the structure of the source tree:
Resources files when accessed using UID are usually of the type Controlname.Property for which you want to bind.
So it should be About.Text not About tested And Working

Adding a custom font to a windows phone 8 app

I've read articles such as this 31 days of windows phone day 24 embedding-fonts and this windows 8 xaml tips custom fonts that explain very clearly and simply how to do this.
But for some reason I can't figure out, my custom font doesn't show on my app, whether in the designer view or when I run it.
To be sure I haven't left any step out, I'm showing what I did here for any suggestions on what my issue might be:
1. First, this is my xaml textblock code
<StackPanel Grid.Row="1" x:Name="BottomPanel" Height="120" Width="800" Orientation="Horizontal">
<StackPanel.Background>
<SolidColorBrush Color="{StaticResource PhoneSemitransparentColor}"/>
</StackPanel.Background>
<TextBlock x:Name="MainTextBlock" FontFamily="/Fonts/KOMIKAX_.ttf" TextWrapping="Wrap" Text="Hello" Width="680" Height="120" Margin="10,0,0,0" FontSize="25" Padding="20,10"/>
</StackPanel>
2. I created a folder in my project, called it "Fonts" and that is where I copied my font file.
3. I changed the Build Action of the font file to Content. I left the Copy To Output Directory property as None at first, then changed it to Copy If Newer. No success.
What have I done wrong or what did I leave out?
This work for me.
First make a folder in your App with the name Fonts, then Add font to it, then use the following syntax. I used it, it works perfectly.
<TextBlock FontFamily="/Fonts/Comic.ttf#Comic" TextWrapping="Wrap" Text="Hello" FontSize="25"/>
Make sure that the Build Action must be set to Content,
Right Click on font in folder and click property, then select Build Action as Content, otherwise it will not work on Emulator and Device.
Try using this
<TextBlock x:Name="MainTextBlock" FontFamily=".\Fonts\KOMIKAX_.ttf#Komika Axis" TextWrapping="Wrap" Text="Hello" Width="680" Height="120" Margin="10,0,0,0" FontSize="25" Padding="20,10"/>
In case this doesn't work, remember the format is .\FontPath\FontFileName.ttf#FontName

Why do my images show at designtime and not at runtime?

I have a WPF <Image> that I am trying to display on a <Button> inside of a <Toolbar> with the following code.
<ToolBarTray>
<ToolBar Height="26" Name="toolBar1" VerticalAlignment="Top" >
<Button Name="StampButton" Click="StampButton_Click">
<Image Source="/MyApp.Resources;component/Resources/MyImage.png"
Height="16" Width="16" Stretch="Fill"/>
</Button>
</ToolBar>
</ToolBarTray>
The image shows up just fine at design time. However, at runtime nothing is displayed. The resources are in another dll called MyApp.Resources. The button is actually created just fine and the click event works fine also.
Set your image build action to "Resource". Try to use full source path.
I was getting same problem - image displaying at design time, but not at runtime.
I had build action set to resource, tried different pack uris etc etc, but solution was simple
<Window.Resources>
<BitmapImage x:Key="your_image_key" UriSource="your_image.png" />
<Image Source="{StaticResource your_image_key}"/>
</Window.Resources>
This is what worked for me, from a site called wpf-tutorial.com:
<Image Source="/WpfTutorialSamples;component/Images/copy.png" />
Images is a subfolder of the project folder in my case, and I assume also in this sample.

Change AppBar's menu text

I have the following AppBar.
<Page.BottomAppBar>
<AppBar x:Name="bottomAppBar" Padding="10,0,10,0">
<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button x:Name="switchMeasurementMode" AutomationProperties.Name = "Breath rate" Style="{StaticResource AppBarButtonStyle}" Click="switchMeasurementMode_Click" />
</StackPanel>
</Grid>
</AppBar>
</Page.BottomAppBar>
It looks like this
I tend to change its text during run-time with the following C# code
private void switchMeasurementMode_Click(object sender, RoutedEventArgs e)
{
this.switchMeasurementMode.Name = "111";
}
But the button text is not changed. Is there anything I had missed out?
If you use the default styles for AppBar in Windows 8 C# projects, then you have to change the attached property AutomationProperties.Name either in XAML using:
AutomationProperties.Name = "new name"
or in code using:
Button.SetValue(AutomationProperties.NameProperty, "new value");
or
AutomationProperties.SetName(Button, "new value");
The button is a content control. You set the Content property to change the content.
this.switchMeasurementMode.Content= "111";
The Name property is how you set the programmatic "handle" for the Button. You use the name in the code editor to modify the control. In your case, you are changing the name, which means you loose the ability to say this.switchMeasurementMode...
FYI, content can be more than just text. Most XAML elements can be added as content.

Disable Drag-drop from TextBox

I have a WPF textBox that is declared as ReadOnly
<TextBox IsReadOnly="True" IsTabStop="False" Width="200" />
So, User can not write anything into the TextBox or delete the content but it still allows user to drag the text from this textbox and drop it in any other textbox that caused the text removed from the first textbox(the read-only one) and thats unexpected. Is it a bug?
how can I get rid of this?
I tried the following code :
<StackPanel>
<TextBox IsReadOnly="True">Hello</TextBox>
<TextBox></TextBox>
</StackPanel>
When I drag-and-drop the text (after selection) from the first TexbtBox to the second one, the text is copied, but not removed from the first TextBox. I tried it under .NET 3.5 and .NET 4.0 targets.
If you want to get rid of your bug without trying to understand it (since it shouldn't happen), you can just put an empty control (Canvas will be ok) on top of your TextBox, with its Background property set to transparent.
<Grid>
<TextBox IsReadOnly="True" IsTabStop="False" Width="200" />
<Canvas Background="Transparent"/>
</Grid>
But the text won't be selectable anymore...

Categories

Resources