繁体   English   中英

Xaml设计器在创建DataTemplate类后无法加载

[英]Xaml designer cannot load after creating the DataTemplate Class

我创建了DataTemplate类,如下所示。

namespace WpfApplication2
{
    class TemplateSelector : DataTemplateSelector
    {

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {

                if (item != null && item is TaskList)
                {

                     TaskList list = item as TaskList;
                      Window window = Application.Current.MainWindow;
                       if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(window))
                            return null;
                        if (list.Priority == 1)
                        {
                            return window.FindResource("defaultTemplate") as DataTemplate;
                        }
                        else
                        {
                            return window.FindResource("PriTemplate") as DataTemplate;
                        }





                }
                return base.SelectTemplate(item, container);
        }



    }
}

我已经在窗口资源中创建了两个datatemplate,如下所示。

<WpfApp2:TaskItem x:Key="taskItem" />
<WpfApp2:TemplateSelector x:Key="tempSelector"></WpfApp2:TemplateSelector>
<DataTemplate x:Key="defaultTemplate">
            <Border Name="border" BorderBrush="LightBlue" BorderThickness="1" Padding="5" Margin="5">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition></ColumnDefinition>
                        <ColumnDefinition></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Row="0" Grid.Column="0" Text="Name"></TextBlock>
                    <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Name}" />
                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Item"></TextBlock>
                    <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Item}" />
                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Description"></TextBlock>
                    <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=Description}" />
                </Grid> 
            </Border>
        </DataTemplate>
        <DataTemplate x:Key="PriTemplate">
            <Border BorderBrush="Red" BorderThickness="2" Padding="5" Margin="4">
                <DockPanel HorizontalAlignment="Center">
                    <TextBlock Text="{Binding Description}" Margin="4"></TextBlock>
                    <Image Margin="4,20,20,20" Source="1.jpg"></Image>

                </DockPanel>
            </Border>

        </DataTemplate>

但是在加载设计器后,我得到了以下提示,并且无法加载表单。

System.Reflection.TargetInvocationException
Exception has been thrown by the target of an invocation.
   at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)


System.ArgumentNullException
Value cannot be null.
Parameter name: element

当我运行此应用程序时,它工作正常。 在设计模式下我看不到。 请指教。 谢谢。

我怀疑在设计模式下,Application.Current.MainWindow返回的是null,因此您的FindResource方法调用在null对象上。

尝试以下这一行:

if ((window == null) || (System.ComponentModel.DesignerProperties.GetIsInDesignMode(window)))
    return null;

如果这不起作用,请设置您自己以在设计模式下调试组件。 其中的说明如下: 演练:在设计时调试WPF自定义控件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM