如何访问包含在引用的 UWP 类库中的资源字典?

huangapple go评论74阅读模式
英文:

How to access a resource dictionary contained in a referenced UWP Class Library?

问题

我正在尝试为UWP应用程序实现一个组件库的想法。

我遇到的一个问题是如何从主应用程序中访问组件库中的资源库中的资源。

这是我的测试应用程序的解决方案布局:

如何访问包含在引用的 UWP 类库中的资源字典?

因此,MainPage.Xaml 包含一个 TemplatedControl 的实例,我想为它提供位于资源字典 Dictionary1 中的样式 (TemplatedControlStyle)。

我该如何做?

我尝试过这里的示例:https://stackoverflow.com/questions/338056/resourcedictionary-in-a-separate-assembly 但是那个问题是关于WPF而不是UWP,并且似乎不起作用。

英文:

I'm playing around with the idea of implementing a component library for a UWP app.

One thing I'm struggling with is accessing resources from resource libraries in the component library from within the main application.

Here's the solution layout for my test application:

如何访问包含在引用的 UWP 类库中的资源字典?

So MainPage.Xaml contains an instance of TemplatedControl and I want to give the style (TemplatedControlStyle) which is contained in the resource dictionary Dictionary1.

How would I go about doing it?

I've had a go with the examples from here https://stackoverflow.com/questions/338056/resourcedictionary-in-a-separate-assembly but that question was about WPF rather than UWP and doesn't seem to work

答案1

得分: 1

以下是翻译好的部分:

"我不确定哪种URI方案适用于这种情况,但以下模式应该有效。

<ResourceDictionary Source="ms-appx:///[library name]/Dictionary1.xaml"/>
```"

<details>
<summary>英文:</summary>

I am not sure which category of [URI schemes](https://learn.microsoft.com/en-us/windows/uwp/app-resources/uri-schemes) applies to such case but the following pattern should work.

```xaml
&lt;ResourceDictionary Source=&quot;ms-appx:///[library name]/Dictionary1.xaml&quot;/&gt;

答案2

得分: 1

我使用与你相同的框架创建了一个项目。我使用并验证了以下两种方法,它们可以访问组件库中的资源文件。

<ResourceDictionary Source="ComponentLibraryTest.Components/Dictionary1.xaml" />
   
<ResourceDictionary Source="ms-appx:///ComponentLibraryTest.Components/Dictionary1.xaml" />

顺便说一句,不要忘记将 "ComponentLibraryTest.Components" 添加到你的主项目的引用中。

英文:

I created a project with the same framework as yours. I used and verified the following two methods, and they can access the resource files in the component library.

&lt;ResourceDictionary Source=&quot;ComponentLibraryTest.Components/Dictionary1.xaml&quot; /&gt;
   
&lt;ResourceDictionary Source=&quot;ms-appx:///ComponentLibraryTest.Components/Dictionary1.xaml&quot; /&gt;

By the way, don't forget to add "ComponentLibraryTest.Components" into your main project's References.

huangapple
  • 本文由 发表于 2023年8月4日 23:45:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定