简体   繁体   English

UWP-如何在类库中使用Resources.resw

[英]UWP - How to use Resources.resw in class library

in library 在图书馆

        public string GetName()
    {
        ResourceLoader rl = ResourceLoader.GetForCurrentView("ClassLibrary1/Resources");

        return rl.GetString("Name");
    }

at "ResourceLoader rl = ResourceLoader.GetForCurrentView("ClassLibrary1/Resources");" 在“ ResourceLoader rl = ResourceLoader.GetForCurrentView(“ ClassLibrary1 / Resources”);“

An exception of type 'System.Runtime.InteropServices.COMException' occurred in ClassLibrary1.dll but was not handled in user code ClassLibrary1.dll中发生类型'System.Runtime.InteropServices.COMException'的异常,但未在用户代码中处理

WinRT information: 未找到 ResourceMap。 WinRT信息:未找到ResourceMap。

Additional information: 未找到 ResourceMap。 附加信息:未找到ResourceMap。

未找到 ResourceMap。 未找到ResourceMap。

If there is a handler for this exception, the program may be safely continued. 如果有用于此异常的处理程序,则可以安全地继续执行该程序。

if i add reference this library , it is working well. 如果我添加参考此库,它运行良好。 but i Dynamic reference this library ,it is failure. 但是我动态引用了这个库,这是失败的。

        Assembly assembly = Assembly.Load(new AssemblyName("ClassLibrary1"));
        if (assembly == null)
        {
            return;
        }
        Type type = assembly.GetType("ClassLibrary1.Class1");
        ICore core = Activator.CreateInstance(type) as ICore;
        textBlock1.Text = core.GetName();

        //ClassLibrary1.Class1 c1 = new ClassLibrary1.Class1();
        //textBlock1.Text = c1.GetName(); //it is working well

how to use resources for Dynamic reference in the library? 如何在库中使用资源进行动态引用?

if i add reference this library , it is working well. 如果我添加参考此库,它运行良好。 but i Dynamic reference this library ,it is failure. 但是我动态引用了这个库,这是失败的。

Most of your code is right, you didn't mention what exactly the exception is. 您的大多数代码是正确的,您没有提到确切的异常是什么。 Here is my demo: 这是我的演示:

Portable class library, class1: 便携式类库,class1:

public class Class1
{
    public Class1()
    {
        Debug.WriteLine("ClassLib Loaded!");
    }

    public void Output()
    {
        Debug.WriteLine("ClassLib method!");
    }
}

In UWP app: 在UWP应用中:

Assembly assembly = Assembly.Load(new AssemblyName("ClassLibrary1"));
if (assembly == null)
{
    return;
}
Type type = assembly.GetType("ClassLibrary1.Class1");
object obj = Activator.CreateInstance(type);
var method = type.GetMethod("Output");
method.Invoke(obj, null);

The output in the immediate window is like this: 立即窗口中的输出如下所示:

在此处输入图片说明

To do this, you will need to : 为此,您将需要

  1. build your class library. 建立您的课程库。

  2. right click your class lib, choose "Open folder in File Explorer", in the folder find the "bin" folder => "Debug", copy the ClassLibrary1.dll into your UWP project like this: 右键单击您的类库,选择“在文件资源管理器中打开文件夹”,在该文件夹中找到“ bin”文件夹=>“调试”,将ClassLibrary1.dll复制到您的UWP项目中,如下所示:

在此处输入图片说明

Although doing this can solve the problem, but in my personal opinion, it seems not easier than directly adding reference of this library. 尽管这样做可以解决问题,但是在我个人看来,这似乎比直接添加该库的引用要容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在UWP的类库项目中使用Resources.resw? - How to use Resources.resw in Class library project in UWP? 除了UWP中的Resources.resw外,是否可以使用其他.resw文件进行本地化? - Is it possible to use other .resw files for localization other than Resources.resw in UWP? 如何更改Resources.resw? - How can I change the Resources.resw? 我如何在类库UWP中使用resw资源文件 - how can I use a resw resources file inside a class library UWP 如何使Resources.resw中的字符串显示在设计器中? - How to make strings from Resources.resw show up in the designer? 在uwp上直接从资源(resw)打印| WPF - print from resources (resw) directly in view on uwp | WPF 是否有一种语法可供所有平台使用存储在 uno 跨平台库项目中的 .resw 文件中的本地化资源 - Is there one syntax for all platforms to use localized resources from .resw files stored in uno cross-platform library project Windows Phone 8.1如何加载资源文件RESW - Windows phone 8.1 How to load Resources File RESW 如何在UWP类库中使用XAML静态资源和设计时间数据 - how to use XAML static resources and design time data in UWP class libraries 如何在图书馆中使用全球资源 - How to use Global Resources in a Library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM