using System.ComponentModel;
using System.Windows;
namespace DataBindingDemo {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
DataContext = new EmployeeViewModel(); // 关键:设置数据上下文
}
}
public class EmployeeViewModel : INotifyPropertyChanged {
private string _employeeName = "张三";
private double _salary = 8000;
private bool _isManager;
public string EmployeeName {
get => _employeeName;
set { _employeeName = value; OnPropertyChanged(); }
}
public double Salary {
get => _salary;
set { _salary = value; OnPropertyChanged(); }
}
public bool IsManager {
get => _isManager;
set { _isManager = value; OnPropertyChanged(); }
}
public RelayCommand SubmitCommand => new RelayCommand(_ => {
MessageBox.Show($"已提交: {EmployeeName}, 薪资: {Salary:C2}, 经理: {IsManager}");
});
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string name = null) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
public class RelayCommand : System.Windows.Input.ICommand {
private readonly Action
命令模式集成:ICommand接口实现事件与逻辑分离(如异步操作封装至RelayCommand)
例子:
RelayCommandExample.xaml
MainWindow.xaml.cs
System.Windows;
using System.Threading.Tasks;
namespace CommandDemo {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
DataContext = new MainViewModel();
}
}
public class MainViewModel {
public ICommand LoadDataCommand => new RelayCommand(async _ => {
Status = "加载中...";
await Task.Delay(2000); // 模拟耗时操作
Status = $"数据加载完成 {DateTime.Now:T}";
});
private string _status = "准备就绪";
public string Status {
get => _status;
set { _status = value; OnPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null) {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
public class RelayCommand : ICommand {
private readonly Action _execute;
public RelayCommand(Action execute) => _execute = execute;
public bool CanExecute(object parameter) => true;
public void Execute(object parameter) => _execute(parameter);
public event EventHandler CanExecuteChanged;
}
}
using System.Windows;
using System.Windows.Controls;
namespace TemplateSelectorDemo {
public class CustomTemplateSelector : DataTemplateSelector {
public DataTemplate TextTemplate { get; set; }
public DataTemplate ImageTemplate { get; set; }
public override DataTemplate SelectTemplate(object item,
DependencyObject container) {
return item is TextItem ? TextTemplate :
item is ImageItem ? ImageTemplate : null;
}
}
}
ViewModels.cs
TemplateSelectorDemo {
public abstract class BaseItem {}
public class TextItem : BaseItem {
public string Content { get; set; } = "文本内容";
}
public class ImageItem : BaseItem {
public string Path { get; set; } = "image.png";
}
public class MainViewModel {
public ObservableCollection Items { get; } = new() {
new TextItem(),
new ImageItem(),
new TextItem()
};
}
}
二、高级UI表现力
声明式动画引擎
XAML原生支持时间轴动画与关键帧控制
视觉层级管理
视觉树操作:VisualTreeHelper动态遍历/修改UI元素层级结构
例子:
VisualTreeHelperDemo.xaml
MainWindow.xaml.cs
System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace VisualTreeDemo {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
MainPanel.Children.Add(new TextBlock { Text = "原始元素1" });
MainPanel.Children.Add(new TextBlock { Text = "原始元素2" });
}
private void FindChildren_Click(object sender, RoutedEventArgs e) {
TreeViewer.Items.Clear();
TraverseVisualTree(MainPanel, 0);
}
private void TraverseVisualTree(DependencyObject parent, int depth) {
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) {
var child = VisualTreeHelper.GetChild(parent, i);
TreeViewer.Items.Add(new string(' ', depth*4) + child.GetType().Name);
if (VisualTreeHelper.GetChildrenCount(child) > 0) {
TraverseVisualTree(child, depth + 1);
}
}
}
private void ModifyStyle_Click(object sender, RoutedEventArgs e) {
ApplyStyleToTextBlocks(MainPanel);
}
private void ApplyStyleToTextBlocks(DependencyObject parent) {
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) {
var child = VisualTreeHelper.GetChild(parent, i);
if (child is TextBlock tb) {
tb.Foreground = Brushes.Red;
tb.FontWeight = FontWeights.Bold;
}
ApplyStyleToTextBlocks(child);
}
}
}
}
混合渲染:集成Win2D或SkiaSharp实现GPU加速绘制(如复杂几何图形)
例子:
MainWindow.xaml
MainWindow.xaml.cs
using SkiaSharp;
using System.Windows;
namespace SkiaSharpDemo {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void OnPaintSurface(object sender,
SKPaintSurfaceEventArgs e) {
var surface = e.Surface;
var canvas = surface.Canvas;
// 清空画布
canvas.Clear(SKColors.White);
// 创建渐变画笔
using var paint = new SKPaint {
Shader = SKShader.CreateLinearGradient(
new SKPoint(0, 0),
new SKPoint(e.Info.Width, e.Info.Height),
new[] { SKColors.Blue, SKColors.Red },
new[] { 0f, 1f },
SKShaderTileMode.Clamp)
};
// 绘制复杂路径
using var path = new SKPath();
path.MoveTo(100, 100);
path.CubicTo(300, 50, 200, 200, 400, 150);
path.LineTo(400, 300);
path.ArcTo(200, 200, 0, SKPathArcSize.Large,
SKPathDirection.CounterClockwise, 100, 300);
path.Close();
canvas.DrawPath(path, paint);
}
}
}
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace MauiDemo;
public partial class MainPage : ContentPage, INotifyPropertyChanged
{
private int _count = 0;
private string _counterText = "Click count: 0";
public string CounterText {
get => _counterText;
set {
_counterText = value;
OnPropertyChanged();
}
}
public Command CounterCommand { get; }
public MainPage()
{
InitializeComponent();
CounterCommand = new Command(OnCounterClicked);
BindingContext = this;
}
private void OnCounterClicked()
{
_count++;
CounterText = $"Click count: {_count}";
SemanticScreenReader.Announce(CounterText);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
Platforms/Android/Resources/values/styles.xml
Platforms/iOS/Resources/SecondaryB
using Microsoft.Maui.Controls.PlatformConfiguration;
using Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;
namespace MauiDemo;
public static class SecondaryButtonStyle
{
public static Button ApplySecondaryStyle(this Button button)
{
button.BackgroundColor = Colors.White;
button.TextColor = Colors.Black;
button.On().SetUseSafeArea(true);
return button;
}
}
响应式布局:FlexLayout与Grid结合自适应规则应对屏幕尺寸变化
例子:
ResponsivePage.xaml
ResponsivePage.xaml.cs
using Microsoft.Maui.Controls;
namespace ResponsiveDemo;
public partial class ResponsivePage : ContentPage
{
public ResponsivePage()
{
InitializeComponent();
// 监听尺寸变化
this.SizeChanged += (s,e) => {
bool isWide = this.Width > 600;
VisualStateManager.GoToState(
this,
isWide ? "WideLayout" : "NarrowLayout"
);
};
}
}
App.xaml
原生性能优化
渲染器定制:通过Handler机制重写平台原生控件行为(如Android下定制按钮阴影)
例子:
CustomButton.xaml
CustomButton.xaml.cs
using Microsoft.Maui.Controls;
namespace CustomRendererDemo;
public partial class CustomButton : ContentView
{
public CustomButton()
{
InitializeComponent();
}
}
CustomButtonHandler.cs
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
namespace CustomRendererDemo;
public partial class CustomButtonHandler : ViewHandler
{
protected override Android.Widget.Button CreatePlatformView()
{
var button = new Android.Widget.Button(Context);
return button;
}
protected override void ConnectHandler(Android.Widget.Button platformView)
{
base.ConnectHandler(platformView);
UpdateShadow();
}
void UpdateShadow()
{
if (PlatformView == null) return;
PlatformView.SetShadowLayer(
radius: 10f,
dx: 5f,
dy: 5f,
color: Android.Graphics.Color.Argb(100, 0, 0, 0));
}
}
MauiProgram.cs
using Microsoft.Maui;
using Microsoft.Maui.Hosting;
namespace CustomRendererDemo;
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp()
.ConfigureMauiHandlers(handlers => {
handlers.AddHandler();
});
return builder.Build();
}
}
渲染管线接入:Avalonia框架支持Skia自定义绘制管线实现高性能渲染
例子:
CustomSkiaControl.cs
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Skia;
using SkiaSharp;
public class CustomSkiaControl : Control
{
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
InvalidateVisual();
}
public override void Render(DrawingContext context)
{
var skiaContext = context.GetFeature();
using (var lease = skiaContext.Lease())
{
var canvas = lease.SkCanvas;
canvas.Clear(SKColors.White);
// 绘制渐变背景
using (var paint = new SKPaint())
{
var rect = new SKRect(0, 0, (float)Bounds.Width, (float)Bounds.Height);
paint.Shader = SKShader.CreateLinearGradient(
new SKPoint(0, 0),
new SKPoint((float)Bounds.Width, (float)Bounds.Height),
new[] { SKColors.Blue, SKColors.Green },
new[] { 0f, 1f },
SKShaderTileMode.Clamp);
canvas.DrawRect(rect, paint);
}
// 绘制文本
using (var paint = new SKPaint())
{
paint.Color = SKColors.Red;
paint.TextSize = 24;
paint.IsAntialias = true;
canvas.DrawText("SkiaSharp Rendering", 20, 40, paint);
}
}
}
}
MainWindow.xaml
四、性能与可维护性
渲染优化技术
UI虚拟化:VirtualizingStackPanel应对万级数据列表,仅渲染可视区域元素
例子:
MainWindow.xaml
MainWindow.xaml.cs
using System.Collections.ObjectModel;
using System.Windows;
namespace VirtualizationDemo
{
public partial class MainWindow : Window
{
public ObservableCollection Items { get; set; }
public MainWindow()
{
InitializeComponent();
Items = new ObservableCollection();
// 生成10万条测试数据
for (int i = 0; i < 100000; i++)
{
Items.Add(new DataItem { Name = $"项目 {i + 1}" });
}
DataContext = this;
}
}
public class DataItem
{
public string Name { get; set; }
}
}
异步加载策略:PriorityBinding优先显示关键数据,后台加载次要内容
例子:
MainWindow.xaml
MainWindow.xaml.cs
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows;
namespace PriorityBindingDemo
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private string _loadingText = "加载中...";
public string LoadingText {
get => _loadingText;
set => SetField(ref _loadingText, value);
}
private string _basicInfo;
public string BasicInfo {
get => _basicInfo ?? LoadBasicInfo();
set => SetField(ref _basicInfo, value);
}
private string _fullDescription;
public string FullDescription {
get => _fullDescription ?? Task.Run(LoadFullDescription).Result;
set => SetField(ref _fullDescription, value);
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private string LoadBasicInfo() {
// 模拟快速加载的关键数据
Task.Delay(300).Wait();
return "商品名称:智能手机(基础信息已加载)";
}
private async Task LoadFullDescription() {
// 模拟耗时的详细数据加载
await Task.Delay(3000);
return "产品详情:\n- 6.5英寸AMOLED屏幕\n- 骁龙888处理器\n- 5000mAh电池\n(完整描述已加载)";
}
// INotifyPropertyChanged实现
public event PropertyChangedEventHandler PropertyChanged;
protected void SetField(ref T field, T value, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = null) {
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
开发效率工具链
实时热重载:修改XAML即时预览效果(注:需规避XamlC编译冲突) Live Visual Tree调试:运行时动态检查/修改XAML属性
package gaodai.matrix;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Sc
Asynchronous Http Client是android中非常好的异步请求工具
除了异步之外还有很多封装比如json的处理,cookie的处理
引用
Persistent Cookie Storage with PersistentCookieStore
This library also includes a PersistentCookieStore whi
安装Apache问题:系统找不到指定的文件 No installed service named "Apache2"
每次到这一步都很小心防它的端口冲突问题,结果,特意留出来的80端口就是不能用,烦。
解决方法确保几处:
1、停止IIS启动
2、把端口80改成其它 (譬如90,800,,,什么数字都好)
3、防火墙(关掉试试)
在运行处输入 cmd 回车,转到apa
问题描述:
MongoDB在非正常情况下关闭时,可能会导致索引文件破坏,造成数据在更新时没有反映到索引上。
解决方案:
使用脚本,重建MongoDB所有表的索引。
var names = db.getCollectionNames();
for( var i in names ){
var name = names[i];
print(name);
Zookeeper重载了几个构造函数,其中构造者可以提供参数最多,可定制性最多的构造函数是
public ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, long sessionId, byte[] sessionPasswd, boolea
本文转自:http://hatemysql.com/2010/06/29/select-into-outfile-access-deny%E9%97%AE%E9%A2%98/
为应用建立了rnd的帐号,专门为他们查询线上数据库用的,当然,只有他们上了生产网络以后才能连上数据库,安全方面我们还是很注意的,呵呵。
授权的语句如下:
grant select on armory.* to rn
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Brows
1. I see. 我明白了。2. I quit! 我不干了!3. Let go! 放手!4. Me too. 我也是。5. My god! 天哪!6. No way! 不行!7. Come on. 来吧(赶快)8. Hold on. 等一等。9. I agree。 我同意。10. Not bad. 还不错。11. Not yet. 还没。12. See you. 再见。13. Shut up!
基本事务的使用:
从账户一的余额中转100到账户二的余额中去,如果账户二不存在或账户一中的余额不足100则整笔交易回滚
select * from account;
-- 创建一张账户表
create table account(
-- 账户ID
id number(3) not null,
-- 账户名称
nam