Skip to content

Commit 42dff8a

Browse files
committed
Move the download function into SourceRouter
1 parent c24ff2d commit 42dff8a

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

MainDemo.Wpf/Helper/SourceRouter.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Net;
56
using System.Text;
67
using System.Threading.Tasks;
78
using System.Xml;
@@ -10,7 +11,7 @@
1011
namespace MaterialDesignDemo.Helper {
1112
public class SourceRouter {
1213
private string _typeName;
13-
private const bool GetFromLocalSource = true;
14+
private const bool GetFromLocalSource = false;
1415
public SourceRouter(string typeName) {
1516
_typeName = typeName;
1617
}
@@ -29,8 +30,34 @@ public XmlDocument Local() {
2930
}
3031

3132
public XmlDocument Remote() {
32-
return new MaterialDesignInXamlToolkitGitHubFile(_typeName).GetXmlDocument();
33+
string ownerName = "wongjiahau";
34+
string branchName = "New-Demo-2";
35+
string sourceCode = DownloadFile(
36+
@"https://raw.githubusercontent.com/" +
37+
$"{ownerName}/" +
38+
"MaterialDesignInXamlToolkit/" +
39+
$"{branchName}/"+
40+
"MainDemo.Wpf/" +
41+
$"{_typeName}.xaml"
42+
);
43+
var xmlDoc = new XmlDocument();
44+
xmlDoc.LoadXml(sourceCode);
45+
return xmlDoc;
46+
}
3347

48+
private static string DownloadFile(string sourceUrl)
49+
//Refered from : https://gist.github.com/nboubakr/7812375
50+
{
51+
52+
long existLen = 0;
53+
var httpReq = (HttpWebRequest)WebRequest.Create(sourceUrl);
54+
httpReq.AddRange((int)existLen);
55+
var httpRes = (HttpWebResponse)httpReq.GetResponse();
56+
var responseStream = httpRes.GetResponseStream();
57+
if (responseStream == null) return "Fail to fetch file";
58+
var streamReader = new StreamReader(responseStream);
59+
return streamReader.ReadToEnd();
3460
}
61+
3562
}
3663
}

0 commit comments

Comments
 (0)