我需要显示来自插件(单独的DLL库)的数据模板。我启发了here。
这是我的源代码:
Plugin.cs
public class Plugin
{
private ResourceDictionary ViewTemplate { get; set; } = new ResourceDictionary();
private object ViewModel {get; set;}
public Plugin(){
ViewTemplate = new Uri("PluginExample;component/View.xaml", UriKind.RelativeOrAbsolute);
ViewModel = new ExampleVm() {Title = "Example"};
}
public Result GetResult()
{
return new Result()
{
Content = new Content() { Template = ViewTemplate, ViewModel = ViewModel};
}
}
}
View.xaml
<ResourceDictionary>
<DataTemplate DataType="{x:Type vm:ExampleVm}">
<TextBlock Text="{Binding Title}" Background="Red"/>
</DataTemplate>
</ResourceDictionary>
ApplicationVm
public ApplicationVm()
{
var result = Plugin.GetResult();
Application.Current.Resources.MergedDictionaries.Add(result.Content.Template);
ResultVm = result.ViewModel;
}
ApplicationView
<ContentControl Content="{Binding ResultVm}" />
启动应用程序时,将显示Namespace.ExampleVm。 正确的解决方案必须是绑定了ExampleVm.Title =“ Example”的TextBlock。 在哪里可以解决问题?如何从插件显示DataTemplate?谢谢