Colors.XAML具有以下代码:
<?xml version="1.0" encoding="UTF-8" ?>
<?xaml-comp compile="true" ?>
<ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Project2.Resources.Colors">
<Color x:Key="TestColor">#512BD4</Color>
</ResourceDictionary>
目标是在Project1中使用
TestColor
,也许是在Project3,Project4等中使用。只要标准wpf样XAML符号在MAUI
中不起作用,我们就必须包括以下文件:
Project1/app.xaml:<?xml version="1.0" encoding="utf-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:resources="clr-namespace:Project2.Resources;assembly=Project2"
x:Class="Project1.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<resources:Colors />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
这个至少可以在没有错误的情况下进行编译,但这行不通。当我试图在某些视野之王中使用新的静态资源时,例如:
Project1/mainwindow.xaml:
<Grid BackgroundColor="{StaticResource TestColor}" />
它引发了一个运行时错误,而IntelliSense对TestColor So so so so so not。问题是:我需要如何使用毛伊岛的其他项目正确地链接我的外部资源计算?
update:不再需要这个工作。
工作量:在应用程序启动期间,手动将DLL资源中的所有条目复制为
Microsoft.Maui.Controls.Xaml.XamlParseException: 'Position XX:XX. StaticResource not found for key TestColor'
.Application.Current.Resources
:中
App.xaml.cs
todo:代码获取DLL的ResourceCtionary。 我没有在毛伊岛的DLL工作,所以不确定您此步骤所需的代码----------------------------------------------------------
update:现在在毛伊岛支持上面不再需要以上,不完整的工作。 请参见[来自其他组件的合并资源词典](Https://learn.microsoft.com/en-us/dotnet/maui/maui/fundamentals/resource-dictiicalass/Resource-dictionaries?
从粘贴粘贴的链接中编码以便简单参考:
public App()
{
InitializeComponent();
// TODO: Code to get dll's resourcedictionary.
ResourceDictionary rd1 = ...; // Project2.Resources?
// Copy all entries into app's Resources.
foreach (KeyValuePair<string, object> entry in rd1)
{
Application.Current.Resources[entry.Key] = entry.Value;
}
MainPage = new AppShell(); // Or whatever you have here.
}
键点(来自该链接):
外部资源必须有:
构建动作设置为<ContentPage ...
xmlns:local="clr-namespace:ResourceDictionaryDemo"
xmlns:theme="clr-namespace:MyThemes;assembly=MyThemes">
<ContentPage.Resources>
<ResourceDictionary>
<!-- Add more resources here -->
<ResourceDictionary.MergedDictionaries>
<!-- Add more resource dictionaries here -->
<local:MyResourceDictionary />
<theme:DefaultTheme />
<!-- Add more resource dictionaries here -->
</ResourceDictionary.MergedDictionaries>
<!-- Add more resources here -->
</ResourceDictionary>
</ContentPage.Resources>
...
</ContentPage>
代码范围文件[如果只有XAML,就不会发生必要的初始化],
必须在文件的根标签中定义MauiXaml