我有一個用 Avalonia 類別庫撰寫的 ContentControl/TemplatedControl,以及在檔案中定義的樣式。
要在 WPF 中加載樣式,您需要使用此 hack 添加 AssemblyInfo.cs
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
現在有了 Avalonia ......有什么方法可以做到?
編輯:是客戶端必須在 App.xaml 中手動注冊檔案的答案嗎?
<Application.Styles>
<StyleInclude Source="avares://Avalonia.Themes.Default/DefaultTheme.xaml"/>
<StyleInclude Source="avares://Avalonia.Themes.Default/Accents/BaseLight.xaml"/>
<StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Default.xaml"/>
</Application.Styles>
但是——如果我想用不同的樣式顯示多個這樣的控制元件怎么辦?我可以在控制元件上設定一個屬性來選擇主題或顏色。
uj5u.com熱心網友回復:
中定義的樣式App.xaml是全域的,因此所有控制元件都將使用它們。但是,可以在運行時更改它們。在您的情況下,您可以先創建一個樣式字典來簡化事情并將所有內容添加到StyleInclude其中,因此您Application.Styles只有一個條目:
<Application.Styles>
<StyleInclude Source="avares://YourAssembly/Path/YourStyles1.xaml"/>
</Application.Styles>
現在,假設您想YourStyles2.xaml在代碼中將此資源更改為。
private static StyleInclude CreateStyle(string url)
{
var self = new Uri("resm:Styles?assembly=YourAssembly");
return new StyleInclude(self)
{
Source = new Uri(url)
};
}
private void SetStyles()
{
var newStyles = CreateStyle("avares://YourAssembly/Path/YourStyles2.xaml");
Avalonia.Application.Current.Styles[0] = newStyles;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/367396.html
上一篇:熱巧克力架構無加載
下一篇:只有一個Azure函式實體?
