7ddf87655ddf8a546af6be3d72ea49b8a08936de
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / ErrorActivity.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5
6 namespace System.Activities.Presentation
7 {
8     using System;
9     using System.Activities;
10     using System.Activities.Presentation.View;
11     using System.ComponentModel;
12     using System.Runtime;
13     using System.Xaml;
14     using XamlDeferLoad = System.Windows.Markup.XamlDeferLoadAttribute;
15
16     [Designer(typeof(ErrorActivity.ErrorActivityView))]
17     internal class ErrorActivity : Activity
18     {
19         static readonly AttachableMemberIdentifier HasErrorActivitiesProperty =
20             new AttachableMemberIdentifier(typeof(ErrorActivity), "HasErrorActivities");
21         internal const string ErrorNodesProperty = "ErrorNodes";
22
23         [Browsable(false)]
24         [XamlDeferLoad(typeof(NodeListLoader), typeof(object))]
25         public XamlNodeList ErrorNodes { get; set; }
26
27         internal static bool GetHasErrorActivities(object target)
28         {
29             object result;
30             if (AttachablePropertyServices.TryGetProperty(target, HasErrorActivitiesProperty, out result))
31             {
32                 return (bool)result;
33             }
34             return false;
35         }
36
37         internal static void SetHasErrorActivities(object target, bool value)
38         {
39             AttachablePropertyServices.SetProperty(target, HasErrorActivitiesProperty, value);
40         }
41
42         internal static void WriteNodeList(XamlWriter writer, XamlNodeList nodeList)
43         {
44             // We need to pass the ErrorNodes contents through as a NodeList, because XOW doesn't
45             // currently support unknown types, even inside a DeferLoad block.
46             // But if a NodeList is written to XOW as a Value, XOW will unpack, forcing us to re-buffer
47             // the nodes in our deferring loader. So we wrap the NodeList value inside a dummy StartObject.
48             writer.WriteStartObject(XamlLanguage.Object);
49             writer.WriteStartMember(XamlLanguage.Initialization);
50             writer.WriteValue(nodeList);
51             writer.WriteEndMember();
52             writer.WriteEndObject();
53         }
54
55         internal class NodeListLoader : XamlDeferringLoader
56         {
57             public override object Load(XamlReader xamlReader, IServiceProvider serviceProvider)
58             {
59                 // Expects a nodestream produced by WriteNodesList
60                 xamlReader.Read();
61                 xamlReader.Read();
62                 xamlReader.Read();
63                 Fx.Assert(xamlReader.NodeType == XamlNodeType.Value, "Expected Value node");
64                 return (XamlNodeList)xamlReader.Value;
65             }
66
67             public override XamlReader Save(object value, IServiceProvider serviceProvider)
68             {
69                 return ((XamlNodeList)value).GetReader();
70             }
71         }
72
73         internal class ErrorActivityView : WorkflowViewElement
74         {
75             public ErrorActivityView()
76             {
77                 WorkflowViewService.ShowErrorInViewElement(this, SR.ActivityLoadError, null);
78             }
79         }
80     }
81
82     [Designer(typeof(ErrorActivity.ErrorActivityView))]
83     internal class ErrorActivity<T> : Activity<T>
84     {
85         [Browsable(false)]
86         [XamlDeferLoad(typeof(ErrorActivity.NodeListLoader), typeof(object))]
87         public XamlNodeList ErrorNodes { get; set; }
88     }
89 }