毛伊岛:无法在两个SDK式项目之间共享资源计算

发布于 2025-02-09 19:00:46 字数 1895 浏览 3 评论 0原文

我有两个SDK风格的MAUI项目,例如Project1和Project2。 Project2具有以下结构:

Project2.sln
    |
    - Resources
        |
        - Colors.xaml

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-like xaml符号在毛ui中不起作用,我们必须必须将此文件包含如下:

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}" />

它会引发运行时错误microsoft.maui.controls.xaml.xaml.xamlparseexception:'位置xx:xx:xx。对于键testcolor'和Intellisense找不到的静电素,对testColor So so so so so so so so n n n nisthersense没有任何线索。问题是:我需要如何在其他毛伊岛的其他项目中正确链接我的外部资源界面?

I have two SDK-style MAUI projects, let's say Project1 and Project2. Project2 has following structure:

Project2.sln
    |
    - Resources
        |
        - Colors.xaml

Colors.xaml has the following code:

<?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>

The goal is to use TestColor in Project1, maybe later in Project3, Project4, and so on. As long as standard WPF-like XAML notation does not work in MAUI, we have to include this file as following:

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>

This one at least can compile without errors, but that does not work. When i am trying to use new static resource in some king of view, like this:

Project1/MainWindow.xaml:

<Grid BackgroundColor="{StaticResource TestColor}" />

It throws a runtime error Microsoft.Maui.Controls.Xaml.XamlParseException: 'Position XX:XX. StaticResource not found for key TestColor' and IntelliSense having no clue about TestColor so. The question is: how do i need to properly link my external ResourceDictionary in other projects using MAUI?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2025-02-16 19:00:46

解决方法:在应用程序启动期间,手动将DLL中的所有条目复制到application.current.resources
在共享app.xaml.cs中:

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.
}

todo:代码以获取DLL的ResourceCtionary。

我没有在MAUI中与DLL合作,因此不确定您此步骤所需的代码。

Workaround: During app startup, manually copy all the entries in the dll's resourcedictionary into Application.Current.Resources.
In shared App.xaml.cs:

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.
}

TODO: Code to get dll's resourcedictionary.

I haven't worked with dll's in Maui, so not certain the code you need for this step.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文