2014-09-03 2 views
1

저는 Windows Phone 기반 컨트롤을 가지고 있으며 다른 Windows Phone UserControl을 파생 시키려고합니다. UserControl 코드 뒤에WPF에서 파생 된 UserControl의 멤버 컨트롤에 액세스하는 방법

<UserControl x:Class="RML.CtrlBase" 
      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" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid x:Name="LayoutRoot"> 
    </Grid> 
</UserControl> 

자료 :

The name 'LocationConsent_CheckBox' does not exist in the current context 
<path>\project\RML\RML\CtrlLocServ.xaml.cs 

자료 UserControl XAML : 나는에 유도 코드를 넣고 파생 클래스에서 다른 컨트롤에 액세스하려고

, 오류가 발생합니다 :

namespace RML 
{ 
    public partial class CtrlBase : UserControl 
    { 
     public CtrlBase() 
     { 
        InitializeComponent(); 
        LayoutRoot.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Transparent); 
     } 
    } 
} 

유도 된 UserControl xaml : (공지 사항 나는 LayoutRoot2 대신이 LayoutRoot에 그리드의 이름을 변경했습니다.)

<src:CtrlBase x:Class="CtrlLocServ" 
      xmlns:src="clr-namespace:RML" 
      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" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid x:Name="LayoutRoot2"> 
     <StackPanel> 
      <CheckBox x:Name="LocationConsent_CheckBox" /> 
     </StackPanel> 
    </Grid> 
</src:CtrlBase> 

UserControl 파생, 코드 숨김

namespace RML 
{ 
    public partial class CtrlLocServ : CtrlBase 
    { 
     public CtrlLocServ() 
     { 
      InitializeComponent(); 
      LocationConsent_CheckBox.IsChecked = false; 
     } 
    } 
} 

이 여전히하지만 나에게 위의 오류를 제공합니다.

#pragma checksum "..\..\CtrlLocationService.xaml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "D56B477B354FB03C45E5F49343F67196" 
//------------------------------------------------------------------------------ 
// <auto-generated> 
//  This code was generated by a tool. 
//  Runtime Version:4.0.30319.18063 
// 
//  Changes to this file may cause incorrect behavior and will be lost if 
//  the code is regenerated. 
// </auto-generated> 
//------------------------------------------------------------------------------ 

using System; 
using System.Diagnostics; 
using System.Windows; 
using System.Windows.Automation; 
using System.Windows.Controls; 
using System.Windows.Controls.Primitives; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Markup; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Media.Effects; 
using System.Windows.Media.Imaging; 
using System.Windows.Media.Media3D; 
using System.Windows.Media.TextFormatting; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.Windows.Shell; 
namespace RML { 


/// <summary> 
/// CtrlLocationService 
/// </summary> 
public partial class CtrlLocationService : System.Windows.Controls.UserControl, System.Windows.Markup.IComponentConnector { 

    private bool _contentLoaded; 

    /// <summary> 
    /// InitializeComponent 
    /// </summary> 
    [System.Diagnostics.DebuggerNonUserCodeAttribute()] 
    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")] 
    public void InitializeComponent() { 
     if (_contentLoaded) { 
      return; 
     } 
     _contentLoaded = true; 
     System.Uri resourceLocater = new System.Uri("/RML;component/ctrllocationservice.xaml", System.UriKind.Relative); 

     #line 1 "..\..\CtrlLocationService.xaml" 
     System.Windows.Application.LoadComponent(this, resourceLocater); 

     #line default 
     #line hidden 
    } 

     [System.Diagnostics.DebuggerNonUserCodeAttribute()] 
     [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")] 
     [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)] 
     [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")] 
     [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")] 
     [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")] 
     void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) { 
      this._contentLoaded = true; 
     } 
    } 
} 

제안은 많이 주시면 감사하겠습니다 : 여기

생성 된 파일입니다.

+0

'LocationConsent_Checkbox'가 선언 된 위치가 표시되지 않습니다. – Sheridan

+0

에서 파생 된 사용자 컨트롤 Xaml ... – user3174075

+0

좋아, 나는 당신의 문제를 보았다고 생각합니다. 컨트롤을 확장하려면 XAML 파일을 사용할 수 없습니다. 즉,이 방법으로 사용할 XAML 섹션을 정의 할 수 없습니다. 대신 XAML을'DataTemplate'에 정의하고이를'ContentControl's에 적용하여 표시하십시오. – Sheridan

답변

1

변경 :

<UserControl x:Class="CtrlLocationService" 
xmlns:src="clr-namespace:RML.Controls" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
FontFamily="{StaticResource PhoneFontFamilyNormal}" 
FontSize="{StaticResource PhoneFontSizeNormal}" 
Foreground="{StaticResource PhoneForegroundBrush}" 
d:DesignHeight="300" d:DesignWidth="480"> 

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}"> 

    <StackPanel Grid.Row="0" Margin="10,0,10,0"> 
     <CheckBox 
      x:Name="LocationConsent_CheckBox" 
      Content"test" 
      Checked="Location_Checked" 
      Unchecked="Location_Unchecked" /> 

    </StackPanel> 

</Grid> 

</UserControl> 

에 :

<src:CtrlBase x:Class="CtrlLocationService" 
xmlns:src="clr-namespace:RML.Controls" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
FontFamily="{StaticResource PhoneFontFamilyNormal}" 
FontSize="{StaticResource PhoneFontSizeNormal}" 
Foreground="{StaticResource PhoneForegroundBrush}" 
d:DesignHeight="300" d:DesignWidth="480"> 

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}"> 

    <StackPanel Grid.Row="0" Margin="10,0,10,0"> 
     <CheckBox 
      x:Name="LocationConsent_CheckBox" 
      Content"test" 
      Checked="Location_Checked" 
      Unchecked="Location_Unchecked" /> 

    </StackPanel> 

</Grid> 

</src:CtrlBase> 

다른 사람에 대한 기본 사용자 제어를 할 경우 모두 뒤에 코드와 XAML

+0

실제로, xaml과 코드 숨김이 다른 기본 클래스를 지정하면 전혀 컴파일되지 않습니다 – Jogy

+0

잘 xaml 코드에서 그 코드를 놓친 것을 보았 기 때문에 다른 사람에게 도움이 될 것입니다 –

+0

-1 ' 컨트롤을 확장하려면 XAML 파일을 사용하십시오. – Sheridan

0

에 기본을 변경해야 이 작업을 계속하고 작업 코드를 얻으면 업데이트 할 것입니다.

+0

다른 UserControl을 기반으로 UserControl을 만들 수 있습니다. xaml 파일과 코드 뒤에 기본 코드를 지정하면됩니다. – Jogy

+0

Jogy - 개정 된 질문? – user3174075

관련 문제