现在,我们将添加一些代码来使用 Windows App SDK 中的 ResourceManager 类加载字符串资源。
将新的资源文件 (.resw) 项添加到项目(保留 Resources.resw 的默认名称)。
在编辑器中打开资源文件后,使用以下属性创建新的字符串资源。
名称:消息
值:Hello, resources!
保存并关闭资源文件。
打开 Form1.cs(使用 View Code 命令)并编辑事件处理程序,如下所示:
private void button1_Click(object sender, EventArgs e)
{
// Construct a resource manager using the resource index generated during build.
var manager =
new Microsoft.Windows.ApplicationModel.Resources.ResourceManager();
// Look up a string in the resources file using the string's name.
label1.Text = manager.MainResourceMap.GetValue("Resources/Message").ValueAsString;
}
生成项目并运行应用。 单击该按钮可查看显示的字符串 Hello, resources!。
提示
如果在运行时看到一个消息框,指示应用程序需要特定版本的 Windows App Runtime 并询问是否要立即安装,则单击“是”。 这将带你进入 Windows App SDK 最新下载。 有关详细信息,请参阅上面的先决条件部分。
另请参阅运行时体系结构,详细了解应用在使用 Windows App SDK 时采用的 Framework 包依赖项,以及需要在未打包的应用中使用的其他组件。
使用 MSIX 打包并部署 WinForms 应用
某些 Windows 功能和 API(包括 Windows App SDK 通知 API)要求应用在运行时具有包标识(换句话说,应用需要打包)。 有关详细信息,请参阅需要包标识的功能。
在 Visual Studio 的“解决方案资源管理器”中,右键单击解决方案,然后选择“添加”>“新项目...”。
private void button1_Click(object sender, EventArgs e)
{
var notification = new AppNotificationBuilder()
.AddArgument("action", "viewConversation")
.AddArgument("conversationId", "9813")
.AddText("Andrew sent you a picture")
.AddText("Check this out, The Enchantments in Washington!")
.BuildNotification();
AppNotificationManager.Default.Show(notification);
}
生成并重新运行。 单击该按钮,确认显示 Toast 通知。 从运行时缺少包标识的进程调用时,通知 API 将引发异常。