Wpf window title from static resource

僤鯓⒐⒋嵵緔 提交于 2020-01-02 04:33:25

问题


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>

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 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

I have tried to call it in c# code, but i was not succesful.
I know there is option:

<Window.Title></Window.Title>

but there is no argument like text or value, where can i put Text="{StaticResource IT_IS_WORKING}"


回答1:


Just use the more verbose definition of 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>



回答2:


StaticResource are applied at the time of loading of BAML (compiled XAML) into memory and it parses XAML from top to bottom and since your resource haven't created yet, it throws error while loading of XAML.

Instead, try using DynamicResource which is lazy loaded version you can say. It assigns an expression object to target property. This defers looking up the resource until it is needed at runtime.

Read this for further clarification - StaticResource V/S DyanamicResource.

<Window Title="{DynamicResource IT_WILL_WORK}"/>


来源:https://stackoverflow.com/questions/19840044/wpf-window-title-from-static-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!