【发布时间】:2013-03-10 00:11:57
【问题描述】:
好的,我们现在正在为我们的 GUI 试用 XAML(并在学习过程中学习)...我已经能够在 XAML 和 C# 中相互独立地进行数据绑定而不会出现问题,但现在是时候了我需要来回传递值,我有点迷茫。当我编译并尝试导航到页面时,它会抛出 XamlParseException:指定的类名与实际的根实例类型不匹配。删除 Class 指令或通过 XamlObjectWriterSettings.RootObjectInstance 提供实例。第 5 行位置 2。
非常感谢任何帮助或在正确方向上轻轻推动:)
我在这里:
namespace TheAirline.GraphicsModel.PageModel.PageFinancesModel
{
/// <summary>
/// Interaction logic for PageFinances.xaml
/// </summary>
public partial class PageFinances : Page
{
private Airline Airline;
public PageFinances(Airline airline)
{
InitializeComponent();
this.Language = XmlLanguage.GetLanguage(new CultureInfo(AppSettings.GetInstance().getLanguage().CultureInfo, true).IetfLanguageTag);
this.Airline = airline;
Page page = null;
//loading the XAML
using (FileStream fs = new FileStream("TheAirline\\GraphicsModel\\PageModel \\PageFinancesModel\\PageFinances.xaml", FileMode.Open, FileAccess.Read))
{
page = (Page)XamlReader.Load(fs);
}
//finding XAML element and trying to set the value to a variable
string airlineCash = GameObject.GetInstance().HumanAirline.Money.ToString();
TextBox cashValue = (TextBox)page.FindName("cashValue");
cashValue.DataContext = airlineCash;
}
}
}
还有 XAML 的前几行:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:AirlineModel="clr-namespace:TheAirline.Model.AirlineModel"
mc:Ignorable="d"
x:Class="TheAirline.GraphicsModel.PageModel.PageFinancesModel.PageFinances"
xmlns:c="clr-namespace:TheAirline.GraphicsModel.Converters"
...>
</Page>
【问题讨论】:
-
你的说法
I have been able to do the data binding without a problem in XAML and C# independent of one another, but now comes the time I need to pass values back and forth and I'm a bit lost是完全矛盾的。如果您DataBind,则DataBindingITSELF正在来回传递数据。我不明白你的意思。此外,您不应该做TextBox cashValue = (TextBox)page.FindName("cashValue");和cashValue.DataContext = airlineCash;之类的事情。请说明您的需求,我们可以为您提供帮助。 -
对不起,如果这有点含糊(而且自相矛盾) - 仍在学习 XAML 的来龙去脉。无论如何 - 我的意思是我能够将滑块的值绑定到 XAML 或 c# 中的文本框,但不能将在 XAML 中创建的值绑定到 c# 中的变量,反之亦然。我希望这更有意义:)
标签: c# xaml visual-studio-2012 .net-4.5