我希望制作一个类似于Android屏幕的窗口,显示蓝牙、wifi等状态。我有一个检查设备状态并返回0表示关闭、1表示开启和0xFF表示错误的字节值的类。然后我创建了一个按钮(可能应该是ToggleButton...但我对WPF非常陌生)
public class ToggleTaskButton : System.Windows.Controls.Primitives.ButtonBase
{
public ImageSource ImageSource
{
get { return (ImageSource)GetValue(ImageSourceProperty); }
set { SetValue(ImageSourceProperty, value); }
}
public Color MyBackgroundColor
{
get { return (Color)GetValue(MyBackgroundColorProperty); }
set { SetValue(MyBackgroundColorProperty, value); }
}
// Using a DependencyProperty as the backing store for MyBackgroundColor. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyBackgroundColorProperty =
DependencyProperty.Register("MyBackgroundColor", typeof(Color), typeof(ToggleTaskButton), new PropertyMetadata(null));
// Using a DependencyProperty as the backing store for ImageSource. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ToggleTaskButton), new UIPropertyMetadata(null));
}
关于上述类的注释是,我在考虑是否需要一个依赖属性?相反,我更愿意设置一个值,背景颜色会变为适当的颜色。0 会是灰色,1 会是绿色,>1 会是红色。有一件事我不知道如何做。
然后我制作了一个蓝牙 UserControl,然后将类型改为 ToggleTaskButton。该项目仅为类库,因此我没有获得资源字典:/ 我正在尝试在发布之前使按钮单击部分正常工作。对于混乱造成的不便,敬请谅解。
<ata:ToggleTaskButton x:Class="AdvancedTaskAssigner.Controls.BluetoothControl"
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:ata="clr-namespace:AdvancedTaskAssigner.Controls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Loaded="UserControl_Loaded" Click="ToggleTaskButton_Click"
ImageSource="/AdvancedTaskAssigner;component/Resources/Bluetooth.png" MyBackgroundColor="Green">
<ata:ToggleTaskButton.Resources>
<Style TargetType="{x:Type ata:ToggleTaskButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ata:ToggleTaskButton}">
<Viewbox>
<Grid>
<Border BorderBrush="#FF58595B" BorderThickness="15,15,15,15" CornerRadius="8,8,8,8" >
<Border.Background>
<RadialGradientBrush>
<GradientStop Color="#FFB2B2B2" Offset=".75"/>
<GradientStop Offset="1" Color="#FFB2B2B2" />
</RadialGradientBrush>
</Border.Background>
<Viewbox>
<Image Margin="25" Height="100" Width="100" Source="{TemplateBinding ImageSource}" />
</Viewbox>
</Border>
</Grid>
</Viewbox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ata:ToggleTaskButton.Resources>
</ata:ToggleTaskButton>
代码后台
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace AdvancedTaskAssigner.Controls
{
/// <summary>
/// Interaction logic for BluetoothControl.xaml
/// </summary>
public partial class BluetoothControl : ToggleTaskButton
{
public BluetoothControl()
{
InitializeComponent();
task = new BrightnessTask();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
CheckBluetoothState();
System.Console.Beep(1000, 100);
}
private void CheckBluetoothState()
{
//bluetoothState = ((byte)task.GetState() == 0x01);
//Color c = bluetoothState ? (Color)FindResource("enabledColor") : (Color)FindResource("disabledColor");
//this.outsideColor.Color = c;
}
private BrightnessTask task;
private bool bluetoothState = false;
private void ToggleTaskButton_Click(object sender, RoutedEventArgs e)
{
if (bluetoothState) { task.PerformTaskDown(); MessageBox.Show("BOO"); System.Console.Beep(1000, 100); } //if bluetooth enabled..disable
else { task.PerformTaskUp(); System.Console.Beep(2000, 100); MessageBox.Show("BOO"); }//if bluetooth disabled..enable.
CheckBluetoothState();
}
}
}
所以我不太确定我在做什么。由于我将要处理各种平板电脑和屏幕尺寸,所以我希望使用WPF。我认为,在加载控件时应该使用蓝牙任务来设置状态。当状态被设置时,它会更改边框背景上的第二个渐变停止的颜色。请帮忙。如何设置GradientStop的颜色?当我将此控件添加到WPF应用程序中的UserControl中时,它什么也没有显示,但在我的设计师中,它显示了这3个按钮之一。