簡體   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