.net 在 XAML 中嵌入 System.String

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1821238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:36:17  来源:igfitidea点击:

Embed a System.String in XAML

.netwpfsilverlightxaml

提问by BuddyJoe

Is there a way to embed a string in XAML, give it and ID and refer to it later.

有没有办法在 XAML 中嵌入一个字符串,给它和 ID 并稍后引用它。

I have tried:

我试过了:

    <Window x:Class="WpfApp1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        Title="Window1" Height="300" Width="500">
        <Grid>
            <System:String>Test</System:String>
        </Grid>
    </Window>

And get error:
Cannot add instance of type 'String' to a collection of type 'UIElementCollection'. Only items of type 'UIElement' are allowed.

并得到错误:
无法将“字符串”类型的实例添加到“UIElementCollection”类型的集合中。只允许使用“UIElement”类型的项目。

Could I do this if I nested the String somewhere else in the XAML? or inside a non UI element? Then do I just give it a Name attribute?

如果我将字符串嵌套在 XAML 中的其他位置,我可以这样做吗?还是在非 UI 元素中?那么我是否只给它一个 Name 属性?

回答by Max Galkin

You should use Window.Resources

你应该使用 Window.Resources

Here's an example for Page, in your case it will be Window.Resourcestag:

这是页面的示例,在您的情况下,它将是Window.Resources标签:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:System="clr-namespace:System;assembly=mscorlib">
  <Page.Resources>
    <System:String x:Key="MyString">Hello</System:String>
  </Page.Resources>
  <Grid>  
    <TextBlock Text="{StaticResource MyString}"></TextBlock>
  </Grid>
</Page>

回答by FruityMo

In the Application tag you need to include the following:

在 Application 标签中,您需要包含以下内容:

xmlns:system="clr-namespace:System;assembly=mscorlib">

without the above code, Visual Studio will complain about a missing assembly reference.

如果没有上述代码,Visual Studio 将抱怨缺少程序集引用。

回答by PeterAllenWebb

Having a reference to the string will not allow you to change it later, since strings are immutable, so as Yacoder suggests, just put it in the <Window.Resources>section. Something like:

对字符串的引用将不允许您稍后更改它,因为字符串是不可变的,因此正如 Yacoder 建议的那样,只需将其放在该<Window.Resources>部分即可。就像是:

<Window.Resources>
        <System:String x:Key="TestString">Test</System:String>
</Window.Resources>

If you need to be able to change the value of the string that appears in your grid, you'll want to use a TextBlock or other control whose Content property can be set.

如果您需要能够更改出现在网格中的字符串的值,您需要使用 TextBlock 或其他可以设置 Content 属性的控件。

回答by dotNET

And if you're like me and have typed an unwanted extra character somewhere in the XAML file, you can get this error. Fortunately I had GIT watching over my shoulder, so "Compare with Unmodified" quickly revealed that character that I had mistakenly typed at a place. Hope this can save some hair for you. :)

如果您像我一样在 XAML 文件的某处键入了不需要的额外字符,您可能会收到此错误。幸运的是,我有 GIT 在我的肩膀上监视,所以“与未修改的比较”很快就显示了我在某个地方错误输入的那个字符。希望这可以为您节省一些头发。:)

回答by Amir Mahdi Nassiri

I don't know why, but in my .Net Core 3 WPF app I should use this xmlns definition instead of "mscorlib":

我不知道为什么,但在我的 .Net Core 3 WPF 应用程序中,我应该使用这个 xmlns 定义而不是“mscorlib”:

xmlns:system="clr-namespace:System;assembly=System.Runtime"

then I can define:

然后我可以定义:

<system:Double x:Key="FontSizeLarge">24</system:Double>

or

或者

<system:String x:Key="StringTest">Test</system:String>