.net Micro framework ---WPF1

下载源代码

/**

.net Micro framework ---WPF



在开发一般Windows程序时,有VS设计时的支持。然而,我们在开发.net MF的应用程序时,就没有那么幸运了。
在开发.net MF的应用程序时,vs 没有提供设计时的支持,所以我们必须自己写代码创建一个窗体,窗体上添加控件,等。
写代码创建窗体,控件时,需要我们对窗体的层次结构有所了解。

在下面我们创建一个实例,来明窗体创建的过程。

   Windows|-> StackPanel |-> TextFlow
                                     |-> TextFlow
                      |-> ListBox  |->ListBoxItem

 

code:

---------------------------------------------------------------------------------------------------------

/*
 *


*filename:   Micro Framework Learning
*作者:wolf
*完成日期:2007.11.8
*功能:  -------------------------
*开发工具:vs2005
 
*/

using System;
using System.Collections;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Shapes;

namespace testMFWindowApplication
{
    public class Program : Microsoft.SPOT.Application
    {
  

        private Window mainWindow;

 

        private void OnButtonUp(object sender, ButtonEventArgs e)
        {
            // Print the button code to the Visual Studio output window.
            Debug.Print(e.Button.ToString());
        }
        public static void Main()
        {
            Program myApplication = new Program();

            Window mainWindow = new MainMenuWindow();//myApplication.CreateWindow();

            // Create the object that configures the GPIO pins to buttons.
            GPIOButtonInputProvider inputProvider = new GPIOButtonInputProvider(null);

            // Start the application
            myApplication.Run(mainWindow);
        }
    }
    internal sealed class MainMenuWindow : Window
    {
        private ListBox m_listbox;

        public ListBox MainListBox { get { return m_listbox; } }

        public MainMenuWindow()
          
        {

            Font f = Resources.GetFont(Resources.FontResources.NinaB);

            Color instructionTextColor = ColorUtility.ColorFrom#ffffff;
            Color backgroundColor = ColorUtility.ColorFrom#001200;
            Color unselectedItemColor = ColorUtility.ColorFrom#c0c0ff;   // Unselected listbox item color
            Color selectedItemColor = Colors.Red;                                 // Selected listbox item color

            // The Main window contains a veritcal StackPanel
            StackPanel panel = new StackPanel(Orientation.Vertical);
            this.Child = panel;

            // The top child contains text with instructions
            TextFlow textflow = new TextFlow();
            textflow.TextAlignment = TextAlignment.Left;
            textflow.Visibility = Visibility.Visible;
            textflow.TextRuns.Add(
               new TextRun(Resources.GetString(Resources.StringResources.String1),
               f, instructionTextColor));
            panel.Children.Add(textflow);

            // Add a blank line to the stack
            panel.Children.Add(textflow = new TextFlow());
            textflow.TextRuns.Add(Resources.GetString(Resources.StringResources.Blank), f, instructionTextColor);
            textflow.Visibility = Visibility.Visible;

            // The next child contains a listbox with options
            m_listbox = new ListBox();

            // Prepare the listbox
            Buttons.Focus(m_listbox);
            panel.Children.Add(m_listbox);
            this.Background = m_listbox.Background = new SolidColorBrush(backgroundColor);

            m_listbox.SelectionChanged += delegate(Object sender, SelectionChangedEventArgs e)
            {
                Int32 previousSelectedIndex = e.PreviousSelectedIndex;
                if (previousSelectedIndex != -1)
                {  // If there was a previous index
                    // Change previously-selected listbox item color to unselected color
                    ((Text)m_listbox.Items[previousSelectedIndex].Child).ForeColor = unselectedItemColor;
                }

                // Change newly-selected listbox item color to selected color
                ((Text)m_listbox.Items[e.SelectedIndex].Child).ForeColor = selectedItemColor;
            };
         

            //  Add the items to the listbox
            foreach (String s in new String[] { "Application Setting", "Upload Data", "Down Data", "Start Work" })
            {
                Text text = new Text(f,s);
                text.ForeColor = unselectedItemColor;
                text.TextAlignment = TextAlignment.Center;
          
                ListBoxItem lbi = new ListBoxItem();
                lbi.Background = m_listbox.Background;
                lbi.Child = text;
                m_listbox.Items.Add(lbi);
            }
            m_listbox.SelectedIndex = 0;

            // Add a blank line in the stack
            panel.Children.Add(textflow = new TextFlow());
            textflow.TextRuns.Add(" ", f, instructionTextColor);
            textflow.Visibility = Visibility.Visible;

            //// The bottom child contains text with return instructions
            textflow = new TextFlow();
            textflow.TextAlignment = TextAlignment.Center;
            textflow.Visibility = Visibility.Visible;
            textflow.TextRuns.Add(
               new TextRun("(After viewing a Panel Demo, hit Enter to return to this screen)",
               f, unselectedItemColor));
            panel.Children.Add(textflow);
        }

        protected override void OnButtonDown(ButtonEventArgs e)
        {
            // If <Enter> button is pressed, go into the selected demo

            if (e.Button == Button.Select)
            {
               switch (MainListBox.SelectedIndex) {
            case 0: 
               Debug.Print("你刚刚按下按键:"+ MainListBox.SelectedIndex.ToString());
               break;
            case 1:
                Debug.Print("你刚刚按下按键:" + MainListBox.SelectedIndex.ToString());
               break;
            case 2:
                Debug.Print("你刚刚按下按键:" + MainListBox.SelectedIndex.ToString());
               break;
            case 3:
                Debug.Print("你刚刚按下按键:" + MainListBox.SelectedIndex.ToString());
               break;
         }
            }
         
            // Don't call base implementation (base.OnButtonDown) or we'll go back Home
        }

        protected override void OnGotFocus(FocusChangedEventArgs e)
        {
            // Whenever this window gets focus, it gives it to its listbox
            Buttons.Focus(m_listbox);
            base.OnGotFocus(e);
        }
    }
}

posted on 2008-01-15 16:40  酸辣大白菜  阅读(404)  评论(1编辑  收藏  举报

导航