2010-09-28 23 views
9

मैं पीछे इस कोड है:WPF UserControl सामान्य कोड-पीछे

CustomUserControl.xaml.cs

namespace MyProject 
{ 
    public partial class CustomUserControl<T> : UserControl 
    { 
     ... 
    } 
} 

और इस XAML:

CustomUserControl.xaml

<UserControl x:Class="MyProject.CustomUserControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:sys="clr-namespace:System;assembly=mscorlib"> 
<Grid> 

</Grid> 

यह x: क्लास = "MyProject.CustomUserControl" के बाद से काम नहीं करता है कोड-बैक की जेनेरिक क्लास परिभाषा से मेल नहीं खाता है। क्या यह काम करने का कोई तरीका है?

उत्तर

2

दुर्भाग्य से एक्सएएमएल जेनेरिक कोड का समर्थन नहीं करता है, आप इसके चारों ओर घूम सकते हैं।

नीचे दिए गए लिंक देखें:

http://forums.silverlight.net/forums/p/29051/197576.aspx

Can I specify a generic type in XAML (pre .NET 4 Framework)?

सामान्य नियंत्रण हो सकता है XAML 2009

8

आप सामान्य बना सकते हैं "के साथ दृश्य Studuo के भविष्य के संस्करणों में मूल रूप का समर्थन किया जाएगा कोड-बैक "फ़ाइल के बिना XAML-file:

public class CustomUserControl<T>: UserControl 
{ } 

और यह एक पैरामीटर के रूप में विशिष्ट वर्ग को उपलब्ध कराने से निकाले जाते हैं की तुलना में:

public partial class SpecificUserControl : CustomUserControl<Presenter> 
{ 
    public SpecificUserControl() 
    { 
     InitializeComponent(); 
    } 
} 

XAML:

<application:CustomUserControl 
    x:TypeArguments="application:Presenter" 
    xmlns:application="clr-namespace:YourApplicationNamespace" 
... 

दुर्भाग्य से, ऐसा लगता है कि दृश्य स्टूडियो डिजाइनर दृश्य स्टूडियो 2012 तक इस तरह के जेनरिक का समर्थन नहीं करता अद्यतन 2 (https://stackoverflow.com/a/15110115/355438 देखें)

4

अब तक मेरा समाधान नहीं मिला है। मुख्य अंतर यह है कि जेनेरिक उपयोगकर्ता नियंत्रण वर्ग के लिए मुझे एक .xaml फ़ाइल मिली है और प्रत्येक वास्तविक उपयोगकर्ता नियंत्रण के लिए कोई नहीं है।

GenericUserControl.xaml.cs

using System.Windows.Controls; 

namespace TestGenericUserControl 
{ 
    public abstract partial class GenericUserControl : UserControl 
    { 
     // If you use event handlers in GenericUserControl.xaml, you have to define 
     // them here as abstract and implement them in the generic class below, e.g.: 

     // abstract protected void MouseClick(object sender, MouseButtonEventArgs e); 
    } 

    public class GenericUserControl<T> : GenericUserControl 
    { 
     // generic properties and stuff 

     public GenericUserControl() 
     { 
      InitializeComponent(); 
     } 
    } 

    // To use the GenericUserControl<T> in XAML, you could define: 
    public class GenericUserControlString : GenericUserControl<string> { } 
    // and use it in XAML, e.g.: 
    // <GenericUserControlString /> 
    // alternatively you could probably (not sure) define a markup extension to instantiate 
    // it directly in XAML 
} 

GenericUserControl.xaml

<UserControl x:Class="TestGenericUserControl.GenericUserControl" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d"> 
    <Grid> 
     <Label Content="hello" /> 
    </Grid> 
</UserControl> 
+0

मैं एक तरह से देखते हैं कि आप क्या कर रहे हैं, लेकिन मैं XAML वास्तुकला में सबसे अच्छा नहीं कर रहा हूँ। आप कैसे निर्दिष्ट/कहां निर्दिष्ट करते हैं कि नियंत्रण कैसा दिखना चाहिए? मैंने इस कोड को एक परीक्षण प्रोजेक्ट में कॉपी किया है, लेकिन मैं यह करने में असमर्थ हूं: परिभाषित करें कि यह कैसा दिखना चाहिए, मेनवाइंडो.एक्सएएमएल में इस 'यूजर कंट्रोल' का उपयोग कैसे करें, और डेटा को कैसे बाध्य करें, उदा। 'MyGeneric (T) से 'लेबल' की 'सामग्री' को बांधें। –

+0

@Zach मैंने आपके लिए एक छोटा सा उदाहरण लिखा था। [इस] पर एक नज़र डालें (https://github.com/timmi-on-rails/GenericUserControlWPF)। – Tom

संबंधित मुद्दे