【问题标题】:How to access member controls in a WPF derived UserControl如何访问 WPF 派生的 UserControl 中的成员控件
【发布时间】:2014-09-03 13:36:13
【问题描述】:

我有一个基于 Windows phone 的控件,并希望从中派生其他 Windows phone UserControls。

当我将派生代码放入并尝试从派生类访问其他控件时,出现错误:

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

基础UserControlxaml:

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

我们将不胜感激。

【问题讨论】:

  • 我看不到你的LocationConsent_Checkbox 是在哪里声明的。
  • 在派生用户控件 Xaml ...
  • 好的,我想我看到了你的问题。如果要扩展控件,则不能使用 XAML 文件。这意味着您不能定义要以这种方式使用的 XAML 部分。相反,只需在 DataTemplates 中定义 XAML 并将它们应用到 ContentControls 以​​显示它们。
  • 对不起...我不明白你的意思 - 你能举个例子吗?
  • 通读 MSDN 上的Data Templating Overview 页面,您就会发现。

标签: c# wpf xaml windows-phone-8 user-controls


【解决方案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 中的基本用户控件

【讨论】:

  • 其实,如果xaml和code-behind指定不同的基类,根本就编译不了
  • 我已经看到他在 xaml 代码中错过了这一点,所以它可能会帮助其他人
  • -1 如果要扩展控件,则不能使用 XAML 文件。
【解决方案2】:

仍在努力,当我得到工作代码时,我会更新

【讨论】:

  • 您可以基于另一个 UserControl 创建一个 UserControl,您只需要在 xaml 文件和后面的代码中指定基础
  • Jogy - 你能看看修改后的问题吗?
【解决方案3】:

这适用于我的简单文本示例。您可以尝试通过https://onedrive.live.com/?cid=b37cc7d5117b9a1b&id=B37CC7D5117B9A1B!27341 访问它并检查项目和生成的文件中的差异。

【讨论】:

  • 得到你的源代码......它编译......耶......现在只需要区分
猜你喜欢
  • 1970-01-01
  • 2012-03-05
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-27
  • 2010-12-19
  • 2017-09-30
相关资源
最近更新 更多