我想制作一个类似于 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 为红色。一件事我不知道该怎么做
然后我制作了一个蓝牙用户控件,然后将类型更改为 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,因为我要处理各种各样的平板电脑和各种屏幕尺寸。我在想 OnLoad 控件应该使用蓝牙任务来设置状态。当状态被设置时,它会改变我在边框背景上的第二个渐变停止的颜色。请帮忙。如何设置 GradientStop 的颜色?当我将此控件添加到 WPF 应用程序中的 UserControl 时,它什么也没有显示,但在我的设计器中,它显示了这 3 个按钮之一