【问题标题】:ArgumentException when assigning a resource to CustomMessageBox.Content将资源分配给 CustomMessageBox.Content 时出现 ArgumentException
【发布时间】:2014-08-01 11:58:15
【问题描述】:

我正在使用 Windows Phone Toolkit 开发 Windows Phone 8 应用程序。 使用此扩展提供的 CustomMessageBox 时,您必须将 Content 属性设置为您希望此控件显示的任何内容。 当我将内容设置为在代码中创建的 StackPanel 时,它可以正常工作。但是,当我在 XAML 中创建 StackPanel 时,在 Application.Resources 中是这样的:

<Application.Resources>
    <StackPanel x:Key="MessageBox">
        <TextBlock Text="Teste"/>
    </StackPanel>
</Application.Resources>

并将其分配给 CustomMessageBox

var messageBox = new CustomMessageBox();
messageBox.Content = Application.Current.Resources["MessageBox"];

它抛出一个 ArgumentException 告诉我“值不在预期范围内”。

当我将此 StackPanel 包装到另一个 StackPanel 中时,使用如下代码创建:

var sp = new StackPanel();
sp.Children.Add(Application.Current.Resources["MessageBox"];

这一次它抛出一个 InvalidOperationException 说“元素已经是另一个元素的子元素”。

所以我想我在 XAML 中以错误的方式声明应用程序资源!?

【问题讨论】:

    标签: c# xaml windows-phone-8


    【解决方案1】:

    试试这个。它是 ContentTemplate。

    XAML


    <Application.Resources>
        <local:LocalizedStrings xmlns:local="clr-namespace:PivotApp1" x:Key="LocalizedStrings"/>
        <DataTemplate x:Key="MessageBoxTemp">
            <StackPanel>
                <TextBlock Text="Teste"/>
            </StackPanel>
        </DataTemplate>
    </Application.Resources>
    

    C#


    messageBox.ContentTemplate = (DataTemplate)Application.Current.Resources["MessageBoxTemp"];
    

    【讨论】:

    • 像魅力一样工作!我想知道为什么它不像我那样工作。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    相关资源
    最近更新 更多