Wpf窗口标题从静态资源 [英] Wpf window title from static resource
问题描述
我正在使用资源字典进行本地化,我在wpf中有这个代码:
I am using resource dictionarys for localizations, I have this code in wpf:
<Window x:Class="RWIS_WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="RWIS" Height="500" Width="800" MinHeight="500" MinWidth="800">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Localizations/Dictionary.EN.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
我的问题是,我想使用 {StaticResource mW_screen1 }
My problem is, that I want to localize window title using {StaticResource mW_screen1}
<Window x:Class="RWIS_WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="{StaticResource IT_IS_NOT_WORKING}" Height="500" Width="800"
MinHeight="500" MinWidth="800">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Localizations/Dictionary.EN.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<TextBlock Text="{StaticResource IT_IS_WORKING}"></TextBlock>
但是它不工作,因为资源在标题调用后被定义。它会给我错误,当我尝试运行它
But it is not working, because resource is defined after title is calling it. It will give me error when I try to run it
System.Windows.Markup.XamlParseException发生Message ='提供
值在'System.Windows.StaticResourceExtension'引发异常'
行号'6'和行位置'9'。
System.Windows.Markup.XamlParseException occurred Message='Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '6' and line position '9'.
它正在为标题工作,添加资源后的文本
It is working for headers, text after adding resource
我已经尝试在c#代码中调用它,但是我没有成功。
我知道有选项:
I have tried to call it in c# code, but i was not succesful.
I know there is option:
<Window.Title></Window.Title>
但是没有任何参数,如文本或值,我可以在哪里放文本={StaticResource IT_IS_WORKING}
but there is no argument like text or value, where can i put Text="{StaticResource IT_IS_WORKING}"
推荐答案
只需使用更详细的 StaticResource
:
xmlns:System="clr-namespace:System;assembly=mscorlib"
...
<Window.Resources>
<System:String x:Key="Title">Some Title</System:String>
...
</Window.Resources>
<Window.Title>
<StaticResource ResourceKey="Title" />
</Window.Title>
这篇关于Wpf窗口标题从静态资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!