【问题标题】:XBF Error with DataTemplate in Application.ResourcesApplication.Resources 中的 DataTemplate 出现 XBF 错误
【发布时间】:2015-12-02 21:14:10
【问题描述】:

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

我有一个在我的应用程序的多个页面中使用的 DataTemplate,因此我更愿意编写一次并从我需要的任何地方访问它。为了让任何页面都可以访问它,我把它写在我的App.xaml<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 时出现此错误:

XBF generation error code 0x09c4

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

编辑:根据要求,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();
    }
}

}

【问题讨论】:

  • 能否为您的App.xaml.cs 构造函数添加代码?

标签: xaml windows-runtime datatemplate windows-10 uwp


【解决方案1】:

你可以在20:10最新build session这里找到一些解释 您基本上需要在 XAML 中创建一个资源字典并将一个类附加到它。需要这个类文件来让编译器生成它的代码。

根据您必须做什么以及如何更改代码,您仍然可以使用“旧的”{Binding} 标记,它可以像以前一样工作。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    • 2013-04-19
    • 2018-03-13
    • 1970-01-01
    相关资源
    最近更新 更多