2014-09-03 36 views
1

我有一個基於Windows手機的控件,並且想從其中獲取其他Windows手機UserControl如何訪問WPF派生UserControl中的成員控件

當我把衍生代碼,並試圖從派生類訪問其他控件,我得到一個錯誤:

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

基地UserControl XAML:

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

基地UserControl後面的代碼:

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 2014-09-03 13:56:09

+0

在派生用戶控件Xaml ... – user3174075 2014-09-03 13:57:35

+0

好吧,我認爲我看到你的問題。如果要擴展控件,則不能使用XAML文件。這意味着您無法以這種方式定義XAML的各個部分。相反,只需在'DataTemplate'中定義XAML並將其應用於'ContentControl'來顯示它們。 – Sheridan 2014-09-03 14:00:58

回答

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 2014-09-03 14:06:13

+0

好吧,我已經看到他錯過了xaml代碼,所以它可能會幫助別人 – 2014-09-03 14:08:26

+0

-1您可以'如果要擴展控件,請使用XAML文件。 – Sheridan 2014-09-03 14:09:30

0

改變基地仍然在工作,當我得到的工作代碼,我會更新

+0

你可以創建一個基於另一個UserControl的UserControl,你只需要在xaml文件和代碼後面指定基礎 – Jogy 2014-09-03 14:33:00

+0

Jogy - 你可以看看修改的問題? – user3174075 2014-09-03 15:57:01

相關問題