프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / MarkusAutoUpdate / src / NetSparkle.UI.WPF / UpdateAvailableWindow.xaml.cs @ d8f5045e

이력 | 보기 | 이력해설 | 다운로드 (4.77 KB)

1 d8f5045e taeseongkim
using NetSparkleUpdater.Enums;
2
using NetSparkleUpdater.Events;
3
using NetSparkleUpdater.Interfaces;
4
using NetSparkleUpdater.UI.WPF.Controls;
5
using NetSparkleUpdater.UI.WPF.Interfaces;
6
using NetSparkleUpdater.UI.WPF.ViewModels;
7
using System;
8
using System.Collections.Generic;
9
using System.Linq;
10
using System.Text;
11
using System.Threading;
12
using System.Threading.Tasks;
13
using System.Windows;
14
using System.Windows.Controls;
15
using System.Windows.Data;
16
using System.Windows.Documents;
17
using System.Windows.Input;
18
using System.Windows.Media;
19
using System.Windows.Media.Imaging;
20
using System.Windows.Navigation;
21
using System.Windows.Shapes;
22
23
namespace NetSparkleUpdater.UI.WPF
24
{
25
    /// <summary>
26
    /// Interaction logic for UpdateAvailableWindow.xaml
27
    /// </summary>
28
    public partial class UpdateAvailableWindow : BaseWindow, IUpdateAvailable, IReleaseNotesUpdater, IUserRespondedToUpdateCheck
29
    {
30
        private UpdateAvailableWindowViewModel _dataContext;
31
        private bool _hasFinishedNavigatingToAboutBlank = false;
32
        private string _notes = "";
33
34
        public UpdateAvailableWindow() : base(true)
35
        {
36
            InitializeComponent();
37
        }
38
39
        public UpdateAvailableWindow(UpdateAvailableWindowViewModel viewModel) : base(true)
40
        {
41
            InitializeComponent();
42
            DataContext = _dataContext = viewModel;
43
            _dataContext.ReleaseNotesUpdater = this;
44
            _dataContext.UserRespondedHandler = this;
45
            ReleaseNotesBrowser.Navigated += ReleaseNotesBrowser_Navigated;
46
        }
47
48
        UpdateAvailableResult IUpdateAvailable.Result => _dataContext?.UserResponse ?? UpdateAvailableResult.None;
49
50
        AppCastItem IUpdateAvailable.CurrentItem => CurrentItem;
51
52
        public AppCastItem CurrentItem
53
        {
54
            get { return _dataContext?.Updates?.FirstOrDefault(); }
55
        }
56
57
        public event UserRespondedToUpdate UserResponded;
58
59
        void IUpdateAvailable.BringToFront()
60
        {
61
            BringToFront();
62
        }
63
64
        void IUpdateAvailable.Close()
65
        {
66
            ReleaseNotesBrowser.Navigated -= ReleaseNotesBrowser_Navigated;
67
            CloseWindow();
68
        }
69
70
        void IUpdateAvailable.HideReleaseNotes()
71
        {
72
            if (_dataContext != null)
73
            {
74
                _dataContext.AreReleaseNotesVisible = false;
75
            }
76
            ReleaseNotesRow.Height = new GridLength(10);
77
        }
78
79
        void IUpdateAvailable.HideRemindMeLaterButton()
80
        {
81
            if (_dataContext != null)
82
            {
83
                _dataContext.IsRemindMeLaterVisible = false;
84
            }
85
        }
86
87
        void IUpdateAvailable.HideSkipButton()
88
        {
89
            if (_dataContext != null)
90
            {
91
                _dataContext.IsSkipVisible = false;
92
            }
93
        }
94
95
        void IUpdateAvailable.Show(bool isOnMainThread)
96
        {
97
            ShowWindow(isOnMainThread);
98
        }
99
100
        public void UserRespondedToUpdateCheck(UpdateAvailableResult response)
101
        {
102
            UserResponded?.Invoke(this, new UpdateResponseEventArgs(_dataContext?.UserResponse ?? UpdateAvailableResult.None, CurrentItem));
103
        }
104
105
        // there is some bizarre thing where the WPF browser doesn't navigate to the release notes unless you successfully navigate to
106
        // about:blank first. I don't know why. I feel like this is a Terrible Bad Fix, but...it works for now...
107
108
        public void ShowReleaseNotes(string notes)
109
        {
110
            _notes = notes;
111
            ReleaseNotesBrowser.Dispatcher.Invoke(() =>
112
            {
113
114
                if (ReleaseNotesBrowser.IsLoaded)
115
                {
116
                    if (_hasFinishedNavigatingToAboutBlank)
117
                    {
118
                        ReleaseNotesBrowser.NavigateToString(_notes);
119
                    }
120
                    // else will catch up when navigating to about:blank is done
121
                }
122
                else
123
                {
124
                    // don't do anything until the web browser is loaded
125
                    ReleaseNotesBrowser.Loaded += ReleaseNotesBrowser_Loaded;
126
                }
127
            });
128
        }
129
130
        private void ReleaseNotesBrowser_Loaded(object sender, RoutedEventArgs e)
131
        {
132
            // see https://stackoverflow.com/a/15209861/3938401
133
            ReleaseNotesBrowser.Loaded -= ReleaseNotesBrowser_Loaded;
134
            ReleaseNotesBrowser.Dispatcher.Invoke(() =>
135
            {
136
                ReleaseNotesBrowser.NavigateToString("about:blank");
137
            });
138
        }
139
140
        private void ReleaseNotesBrowser_Navigated(object sender, NavigationEventArgs e)
141
        {
142
            if (!_hasFinishedNavigatingToAboutBlank)
143
            {
144
                ReleaseNotesBrowser.NavigateToString(_notes);
145
                _hasFinishedNavigatingToAboutBlank = true;
146
            }
147
        }
148
    }
149
}
클립보드 이미지 추가 (최대 크기: 500 MB)