2011-05-27 12 views
6

Tengo un control de usuario sin un constructor sin parámetros; llamémoslo WithoutDefaultConstructor. Quiero insertar un WithoutDefaultConstructor llamado myControl en el código XAML de otro control (que se llama MainWindow). Sin embargo, me sale este error del compilador:Nombrar controles de usuario sin constructores predeterminados en XAML

The type 'WithoutDefaultConstructor' cannot have a Name attribute. Value types and types without a default constructor can be used as items within a ResourceDictionary.

¿Cómo puedo solucionar esto sin añadir un constructor sin parámetros a WithoutDefaultConstructor?

Éstos son los contenidos de MainWindow.xaml:

<Window x:Class="WpfApplication1.MainWindow" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:WpfApplication1="clr-namespace:WpfApplication1" Title="MainWindow" 
Height="350" Width="525"> 
    <WpfApplication1:WithoutDefaultConstructor Name="myControl"> 
    </WpfApplication1:WithoutDefaultConstructor> 
</Window> 

Éstos son los contenidos de WithoutDefaultConstructor.xaml.cs:

using System.Windows.Controls; 

namespace WpfApplication1 
{ 
    public partial class WithoutDefaultConstructor : UserControl 
    { 
     private int I_really_need_to_initialize_this_int; 

     public WithoutDefaultConstructor(int i) 
     { 
      InitializeComponent(); 
      I_really_need_to_initialize_this_int = i; 
     } 
    } 
} 

Respuesta

4

Simplemente no lo hacen. En su lugar, exponga una propiedad int en su control de usuario. Si realmente quiere asegurarse de que esté explícitamente establecido, exponga un int? y tírelo si es null.

Cuestiones relacionadas