【问题标题】:XamlParerException: while trying to link text to Xaml pageXamlParerException:尝试将文本链接到 Xaml 页面时
【发布时间】:2014-03-05 23:12:22
【问题描述】:

(Windows Phone 项目)我正在尝试创建一个场景,即用户单击文本并显示 xaml 页面。 该文本称为“条款和条件”。

Event code for the text component
 private void MouseEnter_Agent(object sender, System.Windows.Input.KeyEventArgs e)
    {
        this.NavigationService.Navigate(new Uri("/AgentTerms.xaml", UriKind.Relative));
    }

Xaml 接口代码

 <TextBlock TextWrapping="Wrap" Height="30" Foreground="Red"  MouseEnter="MouseEnter_Agent">
                    <Underline>
                        <Run Text="Read JizAgent Terms and Conditions"/>
                    </Underline>
                    <LineBreak/>
                    <Run/>
                </TextBlock>

单击文本时出现错误 - XamlParerException

【问题讨论】:

  • 您使用的是哪种 XAML 实现(例如 WPF、WinRT XAML)?
  • 它适用于 Windows Phone。

标签: xaml windows-phone-8


【解决方案1】:

您在事件处理程序的签名中使用了错误的 EventArgs 类。你应该使用MouseEventArgs。举个基本的例子:

<Window x:Class="MouseEventArgs.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MouseEventArgs" Height="300" Width="300">

    <Grid x:Name="LayoutRoot"
          Background="Green"
          MouseEnter="Grid_MouseEnter" />
</Window>

背后的代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace MouseEventArgs
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        void Grid_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            LayoutRoot.Background = new SolidColorBrush(Colors.Red);
        }
    }

如果失败,则一定是AgentTerms.xaml 中的标记存在问题。

【讨论】:

  • TextBlock 可以包含多个Inlines。您的标记甚至无法编译,因为 TextBlock 不能包含 StackPanel
  • (只是为了让任何人阅读,我最初建议在TextBlockContent 属性中有多个对象可能会导致异常,但没有意识到它接受多个@ 987654332@ 控制)!
  • 这应该适用于 windows 手机
  • 对不起,我忘了补充这是一个 windows Phone 项目
  • 酷。在这种情况下,我仍然会尝试更新您的事件处理程序以使用 MouseEventArgs 类来查看您是否遇到相同的异常。如果我使用了错误的方法签名,我的 WPF 项目实际上不会编译,但我不确定 Windows Phone 编译器的行为是否相同。
【解决方案2】:
void MouseEnter_Agent(object sender, System.Windows.Input.KeyEventArgs e)

处理程序的签名错误。替换为:

void MouseEnter_Agent(object sender, System.Windows.Input.MouseEventArgs e)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-08
    • 2016-12-12
    • 2014-11-13
    • 2017-02-15
    • 2017-06-03
    • 1970-01-01
    • 2010-11-14
    • 2016-06-12
    相关资源
    最近更新 更多