Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将文本属性随机分配给C#windows应用商店应用程序中的文本块_C#_Windows Store Apps - Fatal编程技术网

将文本属性随机分配给C#windows应用商店应用程序中的文本块

将文本属性随机分配给C#windows应用商店应用程序中的文本块,c#,windows-store-apps,C#,Windows Store Apps,我为50个文本块分配了名称\u1、\u2、\u3、…、\u50,并使用for循环将名称存储在一个数组中。像这样: string[] textname = new string[51]; for(int i = 1; i <= 50; i++) { textname[i] = "_" + i.ToString(); } string[]textname=新字符串[51]; 对于(inti=1;i现在您只有一个字符串数组,您需要的是文本块的数组(或列表,我认为更容易使用) List

我为50个文本块分配了名称
\u1、\u2、\u3、…、\u50
,并使用for循环将名称存储在一个数组中。像这样:

string[] textname = new string[51];
for(int i = 1; i <= 50; i++)
{
     textname[i] = "_" + i.ToString();
}
string[]textname=新字符串[51];

对于(inti=1;i现在您只有一个字符串数组,您需要的是
文本块的数组(或列表,我认为更容易使用)

List<TextBlock> list = new List<TextBlock>();
for (int i = 0; i < 50; i ++)
{
list.Add(new TextBlock() {Text = "something you want to put here", Tag = "_" + i});
}
List List=新列表();
对于(int i=0;i<50;i++)
{
添加(newtextblock(){Text=“你想放在这里的东西”,Tag=““+i});
}
或者,如果确实要使用阵列:

TextBlock[] arr= new TextBlock[51];
for (int i = 0; i <= 50; i ++)
{
arr[i] = new TextBlock() {Text = "something you want to put here", Tag = "_" + i});
}
TextBlock[]arr=新的TextBlock[51];

对于(inti=0;i我认为从代码中引用控件不是一个好主意。我建议使用另一种(MVVM)方法

由于您是按索引访问文本块,我想它们显示在列表中。这意味着您可以在XAML中将它们创建为
项控件

<ItemsControl ItemsSource="{Binding Strings}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
在页面内,您需要将
ViewModel
设置为
DataContext

DataContext = new ViewModel();
您可以直接在
字符串
集合中修改值:

viewModel.Strings[12] = "NewValue";
<TextBlock Text="{Binding Strings[12]}" />
如果出于某种原因,您需要在页面上单独排列文本块,您也可以这样做,并绑定到集合中的正确索引:

viewModel.Strings[12] = "NewValue";
<TextBlock Text="{Binding Strings[12]}" />


虽然在这种情况下,拥有单独的命名属性而不是在集合中为它们编制索引更有意义。

虽然其他答案指出您的方法可能不合适,但我发现仍有必要向您解释如何实际完成您想要完成的任务。您需要使用访问道具请注意,我给你的示例特定于Windows应用商店应用,因为它与其他项目类型略有不同

IEnumerable<PropertyInfo> properties = this.GetType().GetRuntimeProperties();
var textbox = properties.Single(prop =>
    String.Equals(prop.Name, "_1", StringComparison.OrdinalIgnoreCase))
    .GetValue() as TextBox;

if (textbox != null)
{
    var originalValue = textbox.Text;

    textbox.Text = "new value";
}
IEnumerable properties=this.GetType().GetRuntimeProperties();
var textbox=properties.Single(prop=>
String.Equals(prop.Name,“_1”,StringComparison.OrdinalIgnoreCase))
.GetValue()作为文本框;
如果(文本框!=null)
{
var originalValue=textbox.Text;
textbox.Text=“新值”;
}


请注意,您可以用属性名的任何字符串值替换“_1”以获得适当的值
LINQ方法通过假设您总是有一个匹配的属性名称,您的需求可能会有所不同。我还应该指出,虽然这是一个解决方案,但最好重新考虑您的设计,这样您就不必使用反射来访问属性。

您已经有了一个文本块数组吗?可能没有他标记TextBlock的属性以存储所需的字符串?