随笔 - 148  文章 - 1  评论 - 15  阅读 - 30万

Caliburn.Micro代码示例

App.xaml

复制代码
<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
              >
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:HelloBootstrapper x:Key="bootstrapper" />
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>
View Code
复制代码
复制代码
using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp1
{
   public class HelloBootstrapper:BootstrapperBase
    {
        public HelloBootstrapper()
        {
            Initialize();
        }

        protected override void OnStartup(object sender, StartupEventArgs e)
        {
            DisplayRootViewFor<ShellViewModel>();
        }
    }
}
View Code
复制代码

 

HelloBootstrapper类

复制代码
using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp1
{
   public class HelloBootstrapper:BootstrapperBase
    {
        public HelloBootstrapper()
        {
            Initialize();
        }

        protected override void OnStartup(object sender, StartupEventArgs e)
        {
            DisplayRootViewFor<ShellViewModel>();
        }
    }
}
View Code
复制代码

ShellView.xaml

复制代码
<UserControl x:Class="WpfApp1.ShellView"
             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" 
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:cal="http://www.caliburnproject.org"
             xmlns:local="clr-namespace:WpfApp1"
             mc:Ignorable="d" 
             d:DesignHeight="800" d:DesignWidth="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50*"/>
            <RowDefinition Height="50*"/>
            <RowDefinition Height="50*"/>
            <RowDefinition Height="50*"/>
           
            <RowDefinition Height="10*"/>
            <RowDefinition Height="10*"/>
        </Grid.RowDefinitions>

        <StackPanel>
            <!--<RepeatButton Name="IncrementCount" Content="Up" Margin="15" VerticalAlignment="Top" />-->
            <!--<RepeatButton Content="Up" Margin="15"  Grid.Row="3" VerticalAlignment="Top">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        --><!--<cal:ActionMessage MethodName="IncrementCount" />--><!--
                        <cal:ActionMessage MethodName="IncrementCount">
                            <cal:Parameter Value="1" />
                        </cal:ActionMessage>
                    </i:EventTrigger>
                    
                </i:Interaction.Triggers>
                
            </RepeatButton>-->
            <RepeatButton Content="Up" Margin="15" Grid.Row="3" VerticalAlignment="Top"
              cal:Message.Attach="[Event Click] = [Action IncrementCount]" Background="Red"/>
            <TextBlock Grid.Row="0" Name="Count" Margin="20" FontSize="150" VerticalAlignment="Center" HorizontalAlignment="Center" />
            <TextBox Grid.Row="1" x:Name="Name"></TextBox>
            <Button Grid.Row="2" Height="40" x:Name="SayHello" Content="Click Me"></Button>
            <UniformGrid Grid.Row="3" VerticalAlignment="Bottom">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50*"/>
                        <ColumnDefinition Width="50*"/>
                        <ColumnDefinition Width="50*"/>                      
                    </Grid.ColumnDefinitions>
                </Grid>
                <Slider Name="Delta" Minimum="0" Maximum="5" Margin="15" />
                <Button Name="IncrementCount" Content="Increment" Margin="15" />
            </UniformGrid>

        </StackPanel>
    </Grid>
</UserControl>
View Code
复制代码

ShellView.xaml.cs这个文件是默认的

ShellViewModel

复制代码
using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp1
{
    public class ShellViewModel:PropertyChangedBase
    {
        string name;
     public string Name
        {
            get { return name; }
            set { name = value;
                NotifyOfPropertyChange(() => Name);
                NotifyOfPropertyChange(() => CanSayHello);
            }
        }
        public bool CanSayHello
        {
            get { return !string.IsNullOrWhiteSpace(Name); }
        }
        public void SayHello()
        {
            MessageBox.Show(string.Format("Hello{0}!", Name));
        }

        private int _count = 50;

        public int Count
        {
            get { return _count; }
            set
            {
                _count = value;
                NotifyOfPropertyChange(() => Count);
                NotifyOfPropertyChange(() => CanIncrementCount);
            }
        }

        public void IncrementCount()
        {
            Count++;
        }

        public void IncrementCount(int delta)
        {
            Count += delta;
        }
        public bool CanIncrementCount
        {
            get { return Count < 100; }
        }
    }
}
View Code
复制代码

 

posted on   冰魂雪魄  阅读(287)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

WPF框架交流群:C#.net. WPF.core 技术交流�      C#WPF技术交流群:C#.net. WPF.core 技术交流�     WPF技术大牛交流群:C#.net. WPF.core 技术交流�
点击右上角即可分享
微信分享提示