[Silverlight入门系列]实现局部元素全屏(Element部分全屏)

1 篇文章 0 订阅
1 篇文章 0 订阅

[Silverlight入门系列]实现局部元素全屏(Element部分全屏)

本文不讨论Silverlight全屏模式的实现,有关实现这个,可以参考TerryLee的这篇文章,核心代码就是这行:

Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;

本文要讨论的是Silverlight的局部元素全屏,即Element部分全屏。我们在做Silverlight项目中有时候客户有这种需求:希望放大界面的局部,比如一个列表面板啥的,而不是整个界面全屏。如下图:

实现部分全屏

实现起来也是比较简单,主要思路是用Popup实现弹出全屏,把需要全屏的元素放到容器里并设置为屏幕大小,然后整体实现全屏;当用户退出全屏的时候需要把元素恢复回原位置。好,我们写一个ElementsFullScreenProvider类,实现一个UIElement的扩展方法即可。

 

 

主要实现类:ElementsFullScreenProvider
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#region "Using Namespace"
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
#endregion
namespace ElementFullScreen
{
     /// <summary>
     /// Make partial UI elements full screen
     /// Provider features:
     ///    make UI element full screen
     ///    exit full screen and restore UI element
     ///    support UIElement.ToggleElementFullScreen -- extension methods
     /// </summary>
     public static class ElementsFullScreenProvider
     {
         #region Members
         static readonly Popup PopupContainer;
         static readonly Panel PopupContentContainer;
         static ElementController _lastElementController;
         #endregion
         #region ElementController
         class ElementController
         {
             double _height = double .NaN;
             double _width = double .NaN;
             int _lastPanelPosition;
             bool _lastPopupIsOpen;
             readonly DependencyObject _parent;
             Thickness? _margin;
             public UIElement Element { get ; private set ; }
             public ElementController(UIElement element)
             {
                 Element = element;
                 var elem = element as FrameworkElement;
                 if (elem != null && elem.Parent != null )
                 {
                     _parent = elem.Parent;
                 }
             }
             public void BringElementToFullScreen()
             {
                 TryAction<frameworkelement>(Element, f =>
                 {
                     _height = f.Height;
                     _width = f.Width;
                     f.Height = double .NaN;
                     f.Width = double .NaN;
                 });
                 TryAction<control>(Element, f =>
                 {
                     _margin = f.Margin;
                     f.Margin = new Thickness(0);
                 });
                 if (_parent != null )
                 {
                     if (!TryAction<panel>(_parent, p => { _lastPanelPosition = p.Children.IndexOf(Element); p.Children.RemoveAt(_lastPanelPosition); }))
                         if (!TryAction<contentcontrol>(_parent, c => c.Content = null ))
                             if (!TryAction<usercontrol>(_parent, u => u.Content = null ))
                                 TryAction<popup>(_parent, p => { _lastPopupIsOpen = p.IsOpen; p.Child = null ; });
                 }
             }
             public void ReturnElementFromFullScreen()
             {
                 TryAction<frameworkelement>(Element, f =>
                 {
                     f.Height = _height;
                     f.Width = _width;
                 });
                 TryAction<control>(Element, f =>
                 {
                     if (_margin.HasValue)
                     {
                         f.Margin = _margin.Value;
                     }
                 });
                 if (_parent != null )
                 {
                     if (!TryAction<panel>(_parent, p => p.Children.Insert(_lastPanelPosition, Element)))
                         if (!TryAction<contentcontrol>(_parent, c => c.Content = Element))
                             if (!TryAction<usercontrol>(_parent, u => u.Content = Element))
                                 TryAction<popup>(_parent, p => { p.Child = Element; p.IsOpen = _lastPopupIsOpen; });
                 }
             }
             static bool TryAction<t>( object o, Action<t> action)
                 where T : class
             {
                 T val = o as T;
                 if (val != null )
                 {
                     action(val);
                     return true ;
                 }
                 return false ;
             }
         }
         #endregion
         #region FullscreenElementID Attached Property
         static readonly DependencyProperty FullscreenElementIDProperty = DependencyProperty.RegisterAttached(
            "FullscreenElementID" , typeof (Guid?), typeof (ElementsFullScreenProvider), new PropertyMetadata( null ));
         static void SetFullscreenElementID(DependencyObject obj, Guid? value)
         {
             obj.SetValue(FullscreenElementIDProperty, value);
         }
         static Guid? GetFullscreenElementID(DependencyObject obj)
         {
             return (Guid?)obj.GetValue(FullscreenElementIDProperty);
         }
         #endregion
         #region Initialization
         /// <summary>
         /// Initializes the <see cref="ElementsFullScreenProvider"> class.
         /// </see></summary>
         static ElementsFullScreenProvider()
         {
             PopupContentContainer = new Grid();
             PopupContainer = new Popup
             {
                 Child = PopupContentContainer
             };
             Application.Current.Host.Content.FullScreenChanged += delegate
             {
                 if (_lastElementController == null ) return ;
                 if (!Application.Current.Host.Content.IsFullScreen)
                 {
                     ReturnElementFromFullScreen();
                 }
                 else
                 {
                     UpdateContentSize();
                 }
             };
         }
         #endregion
         #region Public Methods
         public static void BringElementToFullScreen( this UIElement element)
         {
             if (_lastElementController == null )
             {
                 _lastElementController = new ElementController(element);
                 _lastElementController.BringElementToFullScreen();
                 PopupContentContainer.Children.Add(element);
                 PopupContainer.IsOpen = true ;
             }
         }
         public static void ReturnElementFromFullScreen( this UIElement element)
         {
             ReturnElementFromFullScreen();
         }
         public static void ReturnElementFromFullScreen()
         {
             if (_lastElementController != null )
             {
                 PopupContentContainer.Children.Clear();
                 _lastElementController.ReturnElementFromFullScreen();
                 PopupContainer.IsOpen = false ;
                 _lastElementController = null ;
             }
         }
         public static void ToggleElementFullScreen( this UIElement element)
         {
             bool newValue = !Application.Current.Host.Content.IsFullScreen;
             bool toggle = false ;
             if (newValue)
             {
                 if (_lastElementController == null )
                 {
                     element.BringElementToFullScreen();
                     toggle = true ;
                 }
             }
             else
             {
                 if (_lastElementController != null && ReferenceEquals(element, _lastElementController.Element))
                 {
                     element.ReturnElementFromFullScreen();
                     toggle = true ;
                 }
             }
             if (toggle)
             {
                 ToggleFullScreen();
             }
         }
         public static void ToggleFullScreen()
         {
             Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
         }
         #endregion
         #region Private Method
         private static void UpdateContentSize()
         {
             if (Application.Current != null && Application.Current.Host != null && Application.Current.Host.Content != null )
             {
                 double height = Application.Current.Host.Content.ActualHeight;
                 double width = Application.Current.Host.Content.ActualWidth;
                 //if (Application.Current.Host.Settings.EnableAutoZoom)
                 //{
                 //    double zoomFactor = Application.Current.Host.Content.ZoomFactor;
                 //    if (zoomFactor != 0.0)
                 //    {
                 //        height /= zoomFactor;
                 //        width /= zoomFactor;
                 //    }
                 //}
                 PopupContentContainer.Height = height;
                 PopupContentContainer.Width = width;
             }
         }
         #endregion
     }
}
</t></t></popup></usercontrol></contentcontrol></panel></control></frameworkelement></popup></usercontrol></contentcontrol></panel></control></frameworkelement>

 

调用的时候使用UIElement的扩展方法实现全屏:
 1: private void button1_Click(object sender, RoutedEventArgs e)
 2: {
 3:  SilverArea/*你希望全屏的元素*/.ToggleElementFullScreen();//调用UIElement的扩展方法
 4:  }
源码下载

本文源码下载请点击此处。主要就一个类ElementsFullScreenProvider.cs,用起来就一个函数ToggleElementFullScreen(),简单之极。

分类: SilverLight
标签: Silverlight
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值