Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何从另一个窗口使用应用程序资源?_Wpf_Xaml_Mvvm - Fatal编程技术网

Wpf 如何从另一个窗口使用应用程序资源?

Wpf 如何从另一个窗口使用应用程序资源?,wpf,xaml,mvvm,Wpf,Xaml,Mvvm,在我的App.xaml文件中,我有: <Application.Resources> <LocalViewModels:SharedSettingsViewModel x:Key="SharedSettingsViewModel"/> <LocalViewModels:ApplicationSpecificSettingsViewModel x:Key="ApplicationSpecificSettingsViewModel" /> <

在我的
App.xaml
文件中,我有:

<Application.Resources>
    <LocalViewModels:SharedSettingsViewModel x:Key="SharedSettingsViewModel"/>
    <LocalViewModels:ApplicationSpecificSettingsViewModel x:Key="ApplicationSpecificSettingsViewModel" />
</Application.Resources>

虽然我同意HighCore的观点,但如果你想这样做,这是你需要做的。下面是一个完整的示例

第一步-创建viewmodel(您似乎已经这样做了)

第二步-将其作为资源添加到app.xaml

<Application x:Class="resourcesTest.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:resourcesTest"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         <local:SharedViewModel x:Key="SharedViewModel"></local:SharedViewModel>
    </Application.Resources>
</Application>

第三步-在窗口中设置datacontext-无论它是哪一个,然后您就可以使用数据了

<Window x:Class="resourcesTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid DataContext="{Binding Source={StaticResource SharedViewModel}}">
        <Label Content="{Binding TestMessage}"></Label>
    </Grid>
</Window>


除非我错过了你想做的事。同样,我不会这样做——我只会将应用程序资源用于样式和特定于UI的事情。希望这能有所帮助

在我看来,ViewModels不属于参考资料,也不属于XAML,因为它们不是一个纯粹的特定于UI的概念。我喜欢VM优先的方法,而不是视图优先的方法。@HighCore那么在这种情况下,它与我的App.cs中的2个静态变量相同吗?
<Application x:Class="resourcesTest.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:resourcesTest"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
         <local:SharedViewModel x:Key="SharedViewModel"></local:SharedViewModel>
    </Application.Resources>
</Application>
<Window x:Class="resourcesTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid DataContext="{Binding Source={StaticResource SharedViewModel}}">
        <Label Content="{Binding TestMessage}"></Label>
    </Grid>
</Window>