wp开发实例(1)——flashlight

本文介绍了一款在WP7平台上开发的手电筒应用,该应用可通过更改背景颜色及控制灯光闪烁来模拟不同模式的手电筒效果。文章详细讲解了如何使用applicationbar、存储技术和timer实现这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    该app在WP7上面开发,主要功能是,根据设置的颜色,改换背景颜色;根据设定的模式,进行灯光的闪烁。相对来说,还是较为容易的。

    主要探讨的技术有:application bar,存储技术,timer。

   

 

    首先定义Setting类,用于存储app的用户设定信息:

     位于Shared/Settings里面:


using System.IO.IsolatedStorage;


namespace WindowsPhoneApp
{
    //存储app用户设置
    public class Setting<T>
    {
        string name;
        T value;
        T defaultValue;
        bool hasValue;


        public Setting(string name, T defaultValue)
        {
            this.name = name;
            this.defaultValue = defaultValue;
        }


        public T Value
        {
            get
            {
                // 检查缓存值
                if (!this.hasValue)
                {
                    //用存储里面取出值
                    if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue(
                          this.name, out this.value))
                    {
                        // 如果还没进行设置
                        this.value = this.defaultValue;
                        IsolatedStorageSettings.ApplicationSettings[this.name] = this.value;
                    }
                    this.hasValue = true;
                }


                return this.value;
            }


            set
            {
                //保存
                IsolatedStorageSettings.ApplicationSettings[this.name] = value;
                this.value = value;
                this.hasValue = true;
            }
        }


        public T DefaultValue
        {
            get { return this.defaultValue; }
        }


        //清空缓存里面的值
        public void ForceRefresh()
        {
            this.hasValue = false;
        }
    }
}


      再接着是MainPage.XAML页面代码:

<phone:PhoneApplicationPage 
    x:Class="FlashLight.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">


    <phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar Opacity="0.5">
            <shell:ApplicationBarIconButton Text="SOS" IconUri="Images/sos.png"
                                            Click="ApplicationBarIconButton_Click"/>
            <shell:ApplicationBarIconButton Text="stroke" IconUri="Images/strobe.png"
                                            Click="ApplicationBarIconButton_Click_1"/>
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="red"/>
                <shell:ApplicationBarMenuItem Text="orange"/>
                <shell:ApplicationBarMenuItem Text="yellow"/>
                <shell:ApplicationBarMenuItem Text="green"/>
                <shell:ApplicationBarMenuItem Text="cyan"/>
                <shell:ApplicationBarMenuItem Text="purple"/>
                <shell:ApplicationBarMenuItem Text="gray"/>
                <shell:ApplicationBarMenuItem Text="white"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>
    
    <Grid x:Name="backgroundGrid" Background="White"/>
    
</phone:PhoneApplicationPage>


    接着是MainPage.XAML.cs里面的逻辑代码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WindowsPhoneApp;


namespace FlashLight
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();


//记住button的位置,在initialize里面没法设置
            this.sosButton = this.ApplicationBar.Buttons[0] as IApplicationBarIconButton;
            this.strobeButton = this.ApplicationBar.Buttons[1] as IApplicationBarIconButton;

//初始化Strobe模式的 timer,下同
            this.strobeTimer.Interval = TimeSpan.FromSeconds(.1);
            this.strobeTimer.Tick += SoberTimer_Tick;


            this.sosTimer.Interval = TimeSpan.Zero;
            this.sosTimer.Tick += SosTimer_Tick;

注册click事件给所有的menuitem
            foreach (IApplicationBarMenuItem menuItem in this.ApplicationBar.MenuItems)
                menuItem.Click += MenuItem_Click;


            this.onBrush = new SolidColorBrush(this.saveColor.Value);
            this.backgroundGrid.Background = this.onBrush;
        }

//application bar 里面的button成员
        IApplicationBarIconButton sosButton;
        IApplicationBarIconButton strobeButton;


        SolidColorBrush onBrush;
        SolidColorBrush offBrush = new SolidColorBrush(Colors.Black);
        DispatcherTimer strobeTimer = new DispatcherTimer();
        DispatcherTimer sosTimer = new DispatcherTimer();
        int sosStep;

//记住设置的颜色,下次启动的时候要用到
        Setting<Color> saveColor = new Setting<Color>("SaveColor", Colors.White);


//当前模式,分为三种
        FlashLightMode mode = FlashLightMode.Solid;


        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            FlashLightMode mode = this.mode;
            RestoreSolidMode();
            
            if (mode == FlashLightMode.Sos)
                return;
            (sender as IApplicationBarIconButton).IconUri = new Uri("Images/cancel.png", UriKind.Relative);
            this.mode = FlashLightMode.Sos;
            this.sosStep = 0;
            this.sosTimer.Start();
        }


        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {

//首先,设置为strobe模式
            FlashLightMode mode = this.mode;
            RestoreSolidMode();


            if (mode == FlashLightMode.Strobe)
                return;

//给出警告
            MessageBoxResult result = MessageBox.Show("strobe light can trigger" +
                "seizures for people with photosensitive epilepsy." +
                "Are you sure you want to start the strobe light?",
                "WARNING!", MessageBoxButton.OKCancel);


            if (result == MessageBoxResult.OK)
            {

//换掉图标,设置模式
                (sender as IApplicationBarIconButton).IconUri = new Uri("Images/cancel.png", UriKind.Relative);
                this.mode = FlashLightMode.Strobe;
                this.strobeTimer.Start();
            }
        }


        private void SoberTimer_Tick(object sender, EventArgs e)
        {

//Timer每次到了之后,换掉颜色
            if (this.backgroundGrid.Background == this.onBrush)
                this.backgroundGrid.Background = this.offBrush;
            else
                this.backgroundGrid.Background = this.onBrush;
        }


        private void SosTimer_Tick(object sender, EventArgs e)
        {
            switch (this.sosStep)
            {
                case 1: case 3: case 5:
                case 13: case 15: case 17:
                    this.backgroundGrid.Background = this.onBrush;
                    this.sosTimer.Interval = TimeSpan.FromSeconds(.2);
                    break;
                case 7: case 9: case 11:
                    this.backgroundGrid.Background = this.onBrush;
                    this.sosTimer.Interval = TimeSpan.FromSeconds(1);
                    break;
                case 18:
                    this.backgroundGrid.Background = this.offBrush;
                    this.sosTimer.Interval = TimeSpan.FromSeconds(1);
                    break;
                default:
                    this.backgroundGrid.Background = this.offBrush;
                    this.sosTimer.Interval = TimeSpan.FromSeconds(.2);
                    break;
            }
            this.sosStep = (this.sosStep + 1) % 19;
        }


        private void MenuItem_Click(object sender, EventArgs e)
        {

//获取每个menuitem的对应的颜色,保存起来
            string chosenColor = (sender as IApplicationBarMenuItem).Text;

//转换成实际的颜色
            Color c = (Color)typeof(Colors).GetProperty(chosenColor,
                BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase).GetValue(null, null);


            this.saveColor.Value = c;
            this.onBrush = new SolidColorBrush(this.saveColor.Value);
            this.backgroundGrid.Background = this.onBrush;
        }


        private void RestoreSolidMode()
        {
            this.strobeTimer.Stop();
            this.sosTimer.Stop();
            this.backgroundGrid.Background = this.onBrush;
            this.strobeButton.IconUri = new Uri("Images/strobe.png",UriKind.Relative);
            this.sosButton.IconUri = new Uri("Images/sos.png", UriKind.Relative);
            this.mode = FlashLightMode.Solid;
        }


        enum FlashLightMode
        {
            Solid,
            Sos,
            Strobe
        }
    }
}



截个图,效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值