相关疑难解决方法(0)

如何调用base.base.method()?

// Cannot change source code
class Base
{
    public virtual void Say()
    {
        Console.WriteLine("Called from Base.");
    }
}

// Cannot change source code
class Derived : Base
{
    public override void Say()
    {
        Console.WriteLine("Called from Derived.");
        base.Say();
    }
}

class SpecialDerived : Derived
{
    public override void Say()
    {
        Console.WriteLine("Called from Special Derived.");
        base.Say();
    }
}

class Program
{
    static void Main(string[] args)
    {
        SpecialDerived sd = new SpecialDerived();
        sd.Say();
    }
}
Run Code Online (Sandbox Code Playgroud)

结果是:

来自Special Derived.
来自Derived./*这不是预期的*/
从Base调用.

如何重写SpecialDerived类,以便不调用中产阶级"Derived"的方法?

更新: 我想继承Derived而不是Base的原因是Derived类包含许多其他实现.既然我不能在 …

c# polymorphism

108
推荐指数
7
解决办法
9万
查看次数

WPF中的全局样式

我正在学习如何在WPF中使用样式,并根据我在网上找到的信息,如果我以这种方式定义样式(使用目标类型):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style  TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Black"/>
    </Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

...此样式将应用于我的应用程序中的所有按钮.然而,似乎我的按钮保持不变.在主窗口中,我将资源字典添加到资源集合中,并插入了几个按钮,但是我的按钮始终看起来一样.这是主窗口的代码:

<Window x:Class="MCTSTrainingChapter1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="GridResources.xaml"/>
                <ResourceDictionary Source="ButtonResources.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <DockPanel >
        <ToolBar DockPanel.Dock="Top" Height="26" Name="toolBar1"  >
            <Button x:Name="btnBold"  Click="Button_Click">Bold</Button>
            <Button Click="Button_Click_1">Italic</Button>
            <Slider Name="slider1" Minimum="2" Maximum="72" Width="100" ValueChanged="slider1_ValueChanged"></Slider>
        </ToolBar>
        <Grid Name="grid1" Background="{StaticResource GridBackgroundBrush}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="100"/>
                <ColumnDefinition Width="5"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <ListBox Grid.Column="0" Name="listBox1" SelectionChanged="listBox1_SelectionChanged" />
            <GridSplitter Grid.Column="1" Margin="0" Width="5" HorizontalAlignment="Left"/>
            <RichTextBox Grid.Column="2" Name="richTextBox1"/>
        </Grid>

    </DockPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

为什么不应用Button样式?

.net c# wpf styles

2
推荐指数
1
解决办法
5509
查看次数

标签 统计

c# ×2

.net ×1

polymorphism ×1

styles ×1

wpf ×1