C# WPF。win xp更改控制内容时应用程序崩溃
我的应用程序具有以下代码:C# WPF。win xp更改控制内容时应用程序崩溃,c#,wpf,xaml,C#,Wpf,Xaml,我的应用程序具有以下代码: void ShuffleRadioAnswersText(List<RadioButton> rad) { List<TextBlock> texts = new List<TextBlock>(); TextBlock content; foreach (var item in rad) { content = (TextBlock)i
void ShuffleRadioAnswersText(List<RadioButton> rad)
{
List<TextBlock> texts = new List<TextBlock>();
TextBlock content;
foreach (var item in rad)
{
content = (TextBlock)item.Content;
texts.Add(content);
}
Random rnd = new Random();
foreach (var item in rad)
{
var txtToAdd = texts[rnd.Next(0, texts.Count)];
item.Content = txtToAdd;
TextBlock txt = item.Content as TextBlock;
texts.Remove(txt);
}
}
我试过一些东西,但没有结果。我知道我可以洗牌TextBlock的文本,但是在一些TextBlock中有inlineCollections(text++text),我不能设置TextBlock的inlineCollection属性,因为它是只读的。
有人有什么想法可以补充吗
编辑:
Xaml部分是:
<StackPanel Grid.Row="1" Grid.Column="1" Margin="10,15,0,0" x:Name="StackAnswers">
<RadioButton Style="{StaticResource radioStyle}" >
<TextBlock Style="{StaticResource txtInRadioStyle}">
SomeText
</TextBlock>
</RadioButton>`<RadioButton Style="{StaticResource radioStyle}" >
<TextBlock Style="{StaticResource txtInRadioStyle}">
SomeText line 1 <LineBreak/> SomeText line 2
</TextBlock>
</RadioButton>
</StackPanel>
一些文字
`
某些文本行1某些文本行2
所以content=(TextBlock)item.content没有问题;
我通过以下方式使其在Windows xp上工作:
List<TextBlock> texts = new List<TextBlock>();
TextBlock content;
foreach (var item in r)
{
content = (TextBlock)item.Content;
texts.Add(content);
}
Random rnd = new Random();
foreach (var item in r)
{
var txtToAdd = texts[rnd.Next(0, texts.Count)];
string resStr = "";
foreach (var it in txtToAdd.Inlines)
{
var run = it as Run;
if (run != null)
resStr += run.Text;
var newline = it as LineBreak;
if (newline != null)
resStr += Environment.NewLine;
}
item.Content = resStr;
texts.Remove(txtToAdd);
}
列表文本=新列表();
文本块内容;
foreach(r中的var项)
{
content=(TextBlock)item.content;
文本。添加(内容);
}
随机rnd=新随机();
foreach(r中的var项)
{
var txtToAdd=文本[rnd.Next(0,text.Count)];
字符串resStr=“”;
foreach(在txtToAdd.Inlines中变量it)
{
var run=将其作为运行;
如果(运行!=null)
resStr+=run.Text;
var newline=它作为换行符;
if(换行符!=null)
resStr+=Environment.NewLine;
}
项目内容=resStr;
文本。删除(txtToAdd);
}
PS:我有windows xp sp3。我不确定,但你应该检查一下 在您的代码中,
item.Content=txtToAdd代码>
此代码是可能的,因为
重要的是
因此,首先,您应该检查您的XP版本
如果ContentPropertyAttribute的关联属性不是string或object类型,则将在运行时调用类型转换器。如果在运行时找不到类型转换器,则会引发异常
我希望这能对您有所帮助。您能提供一些有关崩溃时发生的错误的信息吗?该行的Invalidoperationexception-txtToAdd.ToString()
如果有帮助的话
List<TextBlock> texts = new List<TextBlock>();
TextBlock content;
foreach (var item in r)
{
content = (TextBlock)item.Content;
texts.Add(content);
}
Random rnd = new Random();
foreach (var item in r)
{
var txtToAdd = texts[rnd.Next(0, texts.Count)];
string resStr = "";
foreach (var it in txtToAdd.Inlines)
{
var run = it as Run;
if (run != null)
resStr += run.Text;
var newline = it as LineBreak;
if (newline != null)
resStr += Environment.NewLine;
}
item.Content = resStr;
texts.Remove(txtToAdd);
}