WP开发的基础知识小结

来源:互联网 发布:小米5如何备份数据 编辑:程序博客网 时间:2024/06/06 03:01
(1)C#中string[]数组和list<string>泛型的相互转换

       从string[]转list<string>

          string [] str = { "1" "2" };
             list< string > list = new list< string>(str);


       从list<string>转string[]
             list< string > list=new list< string>;
             string [] str=list.toarray();

(2)关于wp7中的ApplicationBar的图片一直显示不出来

        解决方法:图片的生成操作改为内容。

(3)通过资源类来绑定资源, 步骤:
  a.在程序 App.xml中添加通用类或属性xml:Generic.xaml
    <!--Application Resources-->
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Generic.xaml"/>
                <ResourceDictionary Source="Themes/Res.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

  b. 创建generic.xaml,在其中添加关键属性,该属性对应为程序中的一个类(对象),如下面的LocalizedStrings:
<?xml version="1.0" ?>
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:zds="clr-namespace:Calendar.Resources">

    <zds:LocalizedStrings x:Key="LocalizedStrings"/>  

</ResourceDictionary>  

 c.创建字典对应的类LocalizedStrings,其中Appress是资源类
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Calendar.Resources
{
    public class LocalizedStrings
    {
        public LocalizedStrings() { }

        private static AppRes localizedResources = new AppRes();

        public AppRes LocalizedResources
        {
            get
            {
                return localizedResources;
            }
        }
    }
}

d.实现控件的属性绑定 (path,source)
                        <zdc:ToolsTemplateTile x:Name="test" Grid.Column="0" Grid.Row="0"
                                               TemplateTitle="{Binding Path=LocalizedResources.MenstrualCyclesTitle, Source={StaticResource LocalizedStrings}}"
                                               Click="OnTempalteTileClick" />

(4)跨工程添加资源的问题:http://forums.silverlight.net/t/186827.aspx

     <!--Application Resources-->
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source ="/ZDWorksCommon;component/Themes/Generic.xaml"/>
                <ResourceDictionary Source ="Generic.xaml"/>
                <ResourceDictionary Source ="Themes/Res.xaml"/>              
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

如果加载的路径不正确,vs则会提示

(5)关于计算日期的天数间隔
        一种方法是把日期后面的时分秒置为零:
            a. DateTime t1 = new DateTime(curDate.Year, curDate.Month, curDate.Day);
               DateTime t2 = new DateTime(cmpDate.Year, cmpDate.Month, cmpDate.Day);
               int daysInterval = (t2-t1).Days;

            b. curDate -= curDate.TimeOfDay;
                cmpDate -= cmpDate.TimeOfDay;
                int daysInterval = (curDate-cmpDate).Days;

(6)关于控件样式的等价问题    

 <Style x :Name="ToolsRootGridStyle" TargetType="Grid">
        <Setter Property ="Grid.Background">
            <Setter.Value>
                <ImageBrush ImageSource ="/Images/Common/background.jpg" Stretch="None"/>
            </Setter.Value>
        </Setter>
    </Style>

 <Grid x:Name ="LayoutRoot" Style="{ StaticResource ToolsRootGridStyle}"/>     

等价于:  
<Grid x :Name ="LayoutRoot">
     <Grid.Backgroud>
                < ImageBrush ImageSource ="/Images/Common/background.jpg" Stretch ="None"/>
     </Grid.Backgroud>
</Grid>

(7)自定义Eventhandler的参数
a.定义参数类
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Calendar.Controls
{
    public class AlmanacSelectionChangedEventArgs : EventArgs
    {
        private AlmanacSelectionChangedEventArgs() { }

        internal AlmanacSelectionChangedEventArgs(AlmanacInfo selectedAlmanac)
        {
            SelectedAlmanac = selectedAlmanac;
        }

        public AlmanacInfo SelectedAlmanac { get; private set ; }
    }
}

b.定义委托事件, 装载参数:
      public event EventHandler<AlmanacSelectionChangedEventArgs> SelectionChanged;

         private void OnDoneButtonClick(object sender, EventArgs args)
        {
            SelectionChanged(sender, new AlmanacSelectionChangedEventArgs (m_SelectedItem));
        }

c.注册事件响应,获取参数:

       AlmanacPickerPage almanacPage = e.Content as AlmanacPickerPage;
        if (null != almanacPage)
        {
             almanacPage.SelectionChanged += OnSelectionChanged;
        }

        private void OnSelectionChanged(object sender, AlmanacSelectionChangedEventArgs e)
        {
            m_AlmanacSelected = e.SelectedAlmanac;
        }

(8) 关于ListPicker样式与数据绑定
   首先是声明绑定数据源:
     <Style x :Key="AlmanacLPStyle" TargetType="tk:ListPicker">
        <Setter Property ="FullModeItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Orientation ="Horizontal" Width="480" Height="80" Background ="Blue">
                        <TextBlock x :Name="YiBlock" Text="{Binding YiName}" Foreground ="Green" VerticalAlignment="Center"/>
                        <TextBlock x :Name="JiBlock" Text="{Binding JiName}" Foreground ="Brown" VerticalAlignment="Center" />
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

接着需要做的是定义一个类:
public classs A
{
    public  string YiName {get;set;}
    public  string JiName{get;set;}
}

然后定义一个数据源:
List<A> list = new List<A>();
list.Add(new A() { YiName = "11", JiName= "22" });
list.Add( new A() { YiName = "12" , JiName= "42" });

YiOrJiSelectLP.ItemSource = list;