Application.Resources中的DataTemplate出现XBF错误 [英] XBF Error with DataTemplate in Application.Resources

查看:103
本文介绍了Application.Resources中的DataTemplate出现XBF错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

带有Visual Studio 2015(RTM)的通用Windows平台应用

我有一个用于我应用的多个页面的DataTemplate,因此,我希望只编写一次并从需要的任何地方访问它。为了使其可以在任何页面上访问,我将其写在 App.xaml < Application.Resources>

I have a DataTemplate that is used in multiple pages of my app, so I'd prefer to write it once and access it from anywhere I need to. In order to make it accessible by any page, I write it in my App.xaml's <Application.Resources>:

<Application
x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MyApp"
xmlns:viewmodels="using:MyApp.ViewModels"
RequestedTheme="Light">

<Application.Resources>
    <DataTemplate x:Key="DetailContentTemplate" x:DataType="viewmodels:DataViewModel"> 
    ...
    </DataTemplate>
</Application.Resources>

上面代码的DataTemplate部分在单个页面中可以正常工作,但是当然这意味着我必须将其多次复制并粘贴到其他页面,但这并不高效。但是,在 App.xaml 中使用DataTemplate时出现此错误:

The DataTemplate portion of the code above works just fine in an individual page, but of course that means I'd have to copy and paste it multiple times to other pages, which just isn't efficient. However, I get this error when I use the DataTemplate in App.xaml:

XBF generation error code 0x09c4

我确定这是由于 x:DataType = viewmodels:DataViewModel (没有此功能,因此,没有任何绑定,代码就可以正常工作)。查找错误几乎没有结果。是否有一个方便的解决方法/解决方案,能够在Universal Windows Platform / WinRT应用程序中(最好在XAML中)重用带有绑定的DataTemplate?

I've determined that it stems from the x:DataType="viewmodels:DataViewModel" (without this, and hence, without any bindings the code works just fine). Looking up the error results in next to nothing. Is there a convenient workaround/solution to being able to reuse a DataTemplate with bindings in a Universal Windows Platform/WinRT app, preferably in XAML?

编辑:根据要求,提供App.xaml.cs的完整代码:

As requested, the code in full for App.xaml.cs:

namespace MyApp
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
sealed partial class App : Application
{
    /// <summary>
    /// Allows tracking page views, exceptions and other telemetry through the Microsoft Application Insights service.
    /// </summary>
    public static Microsoft.ApplicationInsights.TelemetryClient TelemetryClient;

    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        TelemetryClient = new Microsoft.ApplicationInsights.TelemetryClient();

        this.InitializeComponent();
        this.Suspending += OnSuspending;
    }

    /// <summary>
    /// Invoked when the application is launched normally by the end user.  Other entry points
    /// will be used such as when the application is launched to open a specific file.
    /// </summary>
    /// <param name="e">Details about the launch request and process.</param>
    protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;

        // Do not repeat app initialization when the Window already has content,
        // just ensure that the window is active
        if (rootFrame == null)
        {
            // Create a Frame to act as the navigation context and navigate to the first page
            rootFrame = new Frame();
            // Set the default language
            rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];

            rootFrame.NavigationFailed += OnNavigationFailed;

            if (rootFrame.Content == null)
            {
                rootFrame.Navigate(typeof(MasterDetailPage));
            }

            // Place the frame in the current Window
            Window.Current.Content = rootFrame;
        }

        // Ensure the current window is active
        Window.Current.Activate();
    }

    /// <summary>
    /// Invoked when Navigation to a certain page fails
    /// </summary>
    /// <param name="sender">The Frame which failed navigation</param>
    /// <param name="e">Details about the navigation failure</param>
    void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
    {
        throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
    }

    /// <summary>
    /// Invoked when application execution is being suspended.  Application state is saved
    /// without knowing whether the application will be terminated or resumed with the contents
    /// of memory still intact.
    /// </summary>
    /// <param name="sender">The source of the suspend request.</param>
    /// <param name="e">Details about the suspend request.</param>
    private void OnSuspending(object sender, SuspendingEventArgs e)
    {
        var deferral = e.SuspendingOperation.GetDeferral();
        //TODO: Save application state and stop any background activity
        deferral.Complete();
    }
}

}

推荐答案

您可以在此处找到有关最新构建会话,时间为20:10
您基本上需要在XAML中创建资源字典并将类附加到该字典。需要使用该类文件,编译器才能生成其代码。

You can find some explanation here from the latest build session at 20:10 You basically need to create a resource dictionary in XAML and attach a class to it. This class file is needed to allow the compiler to generate its code.

根据您必须执行的操作以及如何更改代码,仍然可以使用

Depending on what you have to do and how you can change your code, you can still use the "old" {Binding} markup which will work as before.

<Application.Resources>
    <DataTemplate x:Key="DetailContentTemplate"> 
        <TextBlock Text={Binding myValue} />
    </DataTemplate>
</Application.Resources>

这篇关于Application.Resources中的DataTemplate出现XBF错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆