![](/img/trans.png)
[英]Cannot bind to public property inside ResourceDirectory WinUI3/UWP
[英]how to bind resourcedirectory source property using binding
我在window.xaml
有以下代碼window.xaml
:
<Window x:Class="MD.UI.EntryPoint.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:UICore="http://schemas.MasterData.io/Core/"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MD.UI.Core;component/Themes/Generic.xaml"></ResourceDictionary>
<!--<ResourceDictionary Source="{DynamicResource }"></ResourceDictionary>-->
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Button Name="btnShow" Content="Show" HorizontalAlignment="Left" Margin="329,172,0,0" VerticalAlignment="Top" Width="83" Height="22" Click="btnShow_Click"/>
</Grid>
</Window>
如果您熟悉XAML比你能明白Generic.xaml
在一個單獨的項目定義( dll
)名稱MD.UI.Core
在這個項目中引用,這一切都工作正常 。 意味着Generic.xaml
主題正常工作。
該MD.UI.Core
也有http://schemas.MasterData.io/Core/
XmlnsDefinition
這里使用:
xmlns:UICore="http://schemas.MasterData.io/Core/"
所以,雖然我已經在window.xaml
定義了xmlns:UICore
,但我不想再使用/MD.UI.Core;component/Themes/Generic.xaml
了。 我想要做的是,通過使用xmlns
我想綁定ResourceDirectory.Source
屬性,如:
<ResourceDictionary Source="{DynamicResource }"></ResourceDictionary>
其實我想直接使用UICore
引用之類的
<ResourceDictionary Source="{DynamicResource UICore:component/Themes/Generic.xaml}"></ResourceDictionary>
或something
東西,但使用XAML markup
Binding
表達式。
我不知道。 如何定義以上?
你能幫我么?
我的理解是,您要做的是能夠動態選擇主題(資源字典)。 我通常在C#中這樣做。 例如,假設我們有2個主題。 第一個嵌入在當前組件中,第二個嵌入在外部組件中。 讓我們把他們Internal.xaml和External.xaml從External.dll。 現在我們可以編寫以下方法:
private static void SetStyle(bool useInternal)
{
var res = Application.Current.Resources;
res.MergedDictionaries.Clear();
if (useInternal)
res.MergedDictionaries.Add(new ResourceDictionary()
{
Source = new Uri("pack://application:,,,/Internal.xaml")
});
else
res.MergedDictionaries.Add(new ResourceDictionary()
{
Source = new Uri("pack://application:,,,/External;component/External.xaml")
});
}
此方法已簡化,因為假設MergedDictionaries
集合中只能有一個項目。 但是,它顯示了一個想法。 實際上,您可以相應地向MergedDictionaries
集合添加/刪除資源。
值得指出的是,上面的SetStyle
方法使用全局資源字典。 如果需要,您可以對本地詞典執行相同操作,例如對於特定窗口甚至控件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.