WP7容器内动态嵌套自定义容器类并绑定响应函数的方法

5 篇文章 0 订阅
4 篇文章 0 订阅

重构代码中。。。。。

过去是把所有的控件添加到一个透明图片上,再合并到另一张照片中,好费劲啊,而且不能操作具体的可视元素,那水印是固定的,自己用的都想吐槽。。

大致就是需要在原来的容器类上添加一组包含各个水印元素的子容器,然后对子容器能够完成响应,即添加可视容器并绑定响应函数。

<phone:PhoneApplicationPage 
    x:Class="点击元素并触发消息.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">

    <!--LayoutRoot 是包含所有页面内容的根网格-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel 包含应用程序的名称和页标题-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="我的应用程序" Style="{StaticResource PhoneTextNormalStyle}"/>
            <Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Click="Button_Click"/>
            <TextBlock x:Name="PageTitle" Text="页面名称" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - 在此处放置其他内容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" Tap="ContentPanel_Tap" Background="Red"/>
    </Grid>

</phone:PhoneApplicationPage>

要测试的部分添加到了ContentPanel中,demo嘛,加个button来显示修改的情况,这不重要。。


实现一个控件类,包含一些元素(比如一个容器,Canvas)(但是为毛线最后TextBlock没显示,以后再看吧,困shi了)。canvas设了个黄色的格子。写了一个点击事件(之前构造函数里绑定了,但是在外面似乎不认可啊,所以只在外面绑定了),点一次,true和false翻转。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace 点击元素并触发消息
{
    class CanvasClass
    {
        public Canvas innerCanvas;
        public bool a = false;
        public CanvasClass(object element)
        {
            innerCanvas = new Canvas();
            innerCanvas.Background = new SolidColorBrush(Colors.Yellow);
            if (( element is System.Windows.UIElement ) == true) return;
            innerCanvas.Children.Add((System.Windows.UIElement)element);

        }
        public void canvas_Tap(object sender, GestureEventArgs e)
        {
            e.Handled = true; //如果父容器有其他相同事件的响应就要添加,否则父容器的事件也要在之后发生了。。。
            a = (a==true)?false:true;
        }
    }
}

最后是界面的后台类了,ContentPanel大红格子,点大红格子,打印2333333333,黄格子加button,就显示false和true了

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
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 Microsoft.Phone.Controls;

namespace 点击元素并触发消息
{
    public partial class MainPage : PhoneApplicationPage
    {
        object ad;
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            var tb = new TextBlock();
            tb.Text = "haha";
            tb.FontSize = 30;
            CanvasClass cc = new CanvasClass(tb);
            cc.innerCanvas.Tap += cc.canvas_Tap;
            PageTitle.Text = cc.a.ToString();
            cc.innerCanvas.Width = 400;
            cc.innerCanvas.Height = 400;
            cc.innerCanvas.Margin = new Thickness(0, 0, 0, 2);
            ContentPanel.Children.Add(cc.innerCanvas);
            ad = cc;
        }

        private void ContentPanel_Tap(object sender, GestureEventArgs e)
        {
            PageTitle.Text =  "23333333333";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
            PageTitle.Text = (ad as CanvasClass).a.ToString();

        }
    }
}

今天,一个不平凡的日子,早晨第一次去香港,我又开始写代码了,我也相信爱情了。  -_-大学同学的高中同学的大学同学-_-同学,你说完这些连名字都不说么?这是自我介绍么,以后有机会批评你俩→_→祝幸福~by 单身汉

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值