我有一個帶有MVVM庫的WPF自定義控件庫。在ResourceDictionary中創建對Locator的引用
在字典我有:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfCustomControlLibrary2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d1p1:Ignorable="d"
xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006">
<local:ViewModelLocator x:Key="Locator"
d:IsDataSource="True" />
而在Window.xaml我嘗試使用定位器:
<Window x:Class="WPFCustomeControlLibrary.Window"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Window" Height="300" Width="300"
DataContext="{Binding MainViewModel, Mode=OneWay, Source={StaticResource Locator}}">
<Window.Resources>
<ResourceDictionary Source="Dictionary.xaml" />
</Window.Resources>
<Grid>
</Grid>
但它仍然表示,「資源'定位器'無法解決'。
定位器是我的ViewModelLocator,其中定義了我的MainViewModel。
你爲什麼把它在一個資源字典擺在首位? –
@ThomasV因爲在WPF自定義控件庫中沒有其他選項。 – user7998549