UWP Parse Windows.Storage.StorageFolder and add in a TreeView - c#

I want to parse a StorageFolder that the user picks and create a TreeView which shows its tree structure. This is when I ask to him to choose a file :
private async void browserPathGitDirButton_ClickAsync(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
FolderPicker picker = new FolderPicker();
picker.FileTypeFilter.Add("*");
folder = await picker.PickSingleFolderAsync();
if(folder != null)
{
parser.CreateTreeView(tree, folder);
tree.UpdateLayout();
}
}
After he has picked, I call this method :
public void CreateTreeView(TreeView tree)
{
System.Diagnostics.Debug.WriteLine("root : " + root.Name);
TreeViewItem racine = new TreeViewItem { Header = root.Name };
racine.Tag = root.Path;
tree.Items.Add(ParseStorageAsync(root));
}
And this method too :
public async Task<TreeViewItem> ParseStorageAsync(Windows.Storage.StorageFolder storage)
{
var dirNode = new TreeViewItem { Header = storage.Name };
//directories
foreach (var dir in await storage.GetFoldersAsync())
{
dirNode.Items.Add(ParseStorageAsync(dir));
}
//files
foreach (var file in await storage.GetFilesAsync())
{
TreeViewItem item = new TreeViewItem
{
Header = Path.GetFileNameWithoutExtension(file.Name),
Tag = file.Path
};
dirNode.Items.Add(item);
}
return dirNode;
}
If I use the DirectoryInfo class instead of StorageFolder, it works but only in a WPF project and I have to do an UWP project.
The problem is that it prints this :
because of the asynchronous method...
Is it possible to parse in an other way a folder in an UWP project ?
EDIT : My TreeView class is a library wrote on the Microsoft GitHub so it works well and this is not the problem.

You are adding a Task to the collection without executing it, you need to await your method
tree.Items.Add(await ParseStorageAsync(root));

Related

UWP: MediaPlaybackItem get ViewModel

My MediaPlaybackList.ShuffledItems has 10 items in it. But when I was trying to convert the items back to a list of ViewModel (in my case it is Music) using the uri, I got null.
Why is that? Is it because I load the file from my local drive?
This is how I get the uri:
public static async Task<List<Music>> GetRealPlayList()
{
if (PlayList.ShuffleEnabled)
{
if (ShuffledPlayList.Count == 0)
{
foreach (var music in PlayList.ShuffledItems)
{
ShuffledPlayList.Add(await Music.GetMusic(music.Source.Uri.AbsolutePath));
}
}
return ShuffledPlayList;
}
else
return CurrentPlayList;
}
This is how I set the items:
public static async Task SetPlayList(IEnumerable<Music> playlist)
{
if (Helper.SamePlayList(playlist, CurrentPlayList)) return;
PlayList.Items.Clear();
CurrentPlayList.Clear();
foreach (var music in playlist)
{
var item = new MediaPlaybackItem(MediaSource.CreateFromStorageFile(await Helper.CurrentFolder.GetFileAsync(music.GetShortPath())));
PlayList.Items.Add(item);
CurrentPlayList.Add(music);
}
}
What ways else can I convert the MediaPlackBackItem back to the ViewModel? The GetDisplayProperties().MusicProperties doesn't have the some properties that I want and the properties in it are also empty.
When you create MediaSource,you can set CustomProperties to save the file path in it.And when you loop through the PlayList.ShuffledItems,get file path from the CustomProperties.
Set the items:
MediaSource source = MediaSource.CreateFromStorageFile(await Helper.CurrentFolder.GetFileAsync(music.GetShortPath()));
source.CustomProperties.Add("Path", file.Path);
var item = new MediaPlaybackItem(source);
Get Music class:
foreach (var music in PlayList.ShuffledItems)
{​
MediaSource source = music.Source;​
String path = sour.CustomProperties["Path"].ToString();​
ShuffledPlayList.Add(await Music.GetMusic(path));​
}

View all file from a list

I´m working on a project that uses Caliburn micro in wpf C#.
I´m in the process that I want to rewrite my method ReadMediaFile() so it displays all files in a folder in a list.
My method looks lite this:
private void ReadMediaFile()
{
string result;
_movieviewmodel = new MoviesViewModel();
string[] filePaths = Directory.GetFiles(#"C:/Users/v80770/Desktop/Movies/");
foreach (var file in filePaths)
{
result = Path.GetFileName(file);
_movieviewmodel.MovieName = result;
}
AddItem(_movieviewmodel);
}
When I debug the program all the files show in filePaths but only one shows in my list.
The AddItem is located in a class called TreeViewBase (belongs to caliburn micro I think) and it looks like this:
public void AddItem(T item)
{
_dispatcher.SmartInvoke(() => Items.Add(item));
}
I got the movie files viewing in my list but my MediaUri binding in view is bind against a specific path file but I want it to change dependent on what I choose
I tried to edit the binding to this:
string test = _movieviewmodel.MovieName;
MediaUri = new Uri(test);
But only get a exception "System.UriFormatException: 'Invalid URI: The format of the URI could not be determined.'"
Picture of Uri
New Uri code:
_movieviewmodel.MovieFilePath = #"C:/Users/v80770/Desktop/Movies/";
string test = _movieviewmodel.MovieFilePath;
MediaUri = new Uri(test + _movieviewmodel.MovieName);
But it always shows the same movie and my _movieviewmodel.MovieName does not change name dependent which movie I choose, it always is the same movie.
The creation of a MoviesViewModel item object and AddItem(_movieviewmodel); must be inside foreach, otherwise it would add only the last item:
foreach (var file in filePaths)
{
var movieviewmodel = new MoviesViewModel();
movieviewmodel.MovieName = Path.GetFileName(file);
AddItem(movieviewmodel);
}
or
foreach (var file in filePaths)
{
AddItem(new MoviesViewModel
{
MovieName = Path.GetFileName(file)
});
}

How I can use a DataObject on MacOS (Drag and Drop files)

DataObject is part of System.Windows and not available in Xamarin.Mac. So how can I do a workaround. I want to create a drag n drop function with a external file. On Windows I can do this with a DataObject.
I already searched for alternatives, tried it with NSUrl but without success.
Drag into your app:
All NSView-based classes have a RegisterForDraggedTypes method that you pass what you want to accept (in your case a UTType.FileURL as you are passing something into the app, assumably a file from Finder).
So in this case I have a NSTableView that accepts files, adds them the the table and fires an notification that causes an NSTask to process them (it is a FFMPEG-based task).
tableView.RegisterForDraggedTypes(new string[] { UTType.FileURL };
From there it depends upon the type of NSView you are dragging into, but for example with an NSTableView, you assign its data delegate a NSTableViewDataSource subclass which you have overridden ValidateDrop and AcceptDrop.
In NSTableViewDataSource.ValidateDrop you confirm if the drop contains file(s) that you will accept. In this case, as long as it is a file of any type I accept:
public override NSDragOperation ValidateDrop(NSTableView tableView, NSDraggingInfo info, nint row, NSTableViewDropOperation dropOperation)
{
var operation = NSDragOperation.Copy;
using (var pasteBoard = info.DraggingPasteboard)
{
foreach (var item in pasteBoard.PasteboardItems)
{
if (!item.Types.Contains(UTType.FileURL))
{
operation = NSDragOperation.None;
}
item.Dispose();
}
}
return operation;
}
In in NSTableViewDataSource.AcceptDrop, you actually process the files from the drop.
public override bool AcceptDrop(NSTableView tableView, NSDraggingInfo info, nint row, NSTableViewDropOperation dropOperation)
{
using (var pasteBoard = info.DraggingPasteboard)
{
if (pasteBoard.PasteboardItems.Length > 0)
{
var range = new NSRange(-1, 0);
foreach (var item in pasteBoard.PasteboardItems)
{
if (item.Types.Contains(UTType.FileURL))
{
var finderNode = item.GetStringForType(UTType.FileURL);
// you have a file from macOS' finder, do something with it, assumable in a table view you would add a record/row....
var url = NSUrl.FromString(finderNode);
// url has the file extension, filename, full path, etc...
Post a notification / Add a task to GCD / etc...
}
item.Dispose();
}
return true;
}
}
return false;
}
Drag out of your app:
Lets assume you have an NSView subclass that you wish to drag a "file" out of and into Finder or any app that accepts file drops. Implement the interfaces; INSDraggingSource and INSPasteboardItemDataProvider on your NSView subclass.
In the MouseDown event, start your drag for UTType.FileURL types:
public override void MouseDown(NSEvent theEvent)
{
var pasteboardItem = new NSPasteboardItem();
pasteboardItem.SetDataProviderForTypes(this, new string[1] { UTType.FileURL });
var draggingItem = new NSDraggingItem(pasteboardItem);
var fileDragIcon = new NSImage("theDraggingIcon.png");
draggingItem.SetDraggingFrame(new CoreGraphics.CGRect(0,0,40,40), fileDragIcon);
BeginDraggingSession(new NSDraggingItem[1] { draggingItem }, theEvent, this);
}
In the ProvideDataForType place the file url onto the pasteboard:
public void ProvideDataForType(NSPasteboard pasteboard, NSPasteboardItem item, string type)
{
if (type == UTType.FileURL )
{
var url = new NSUrl("/Users/Sushi/Desktop/StackOverflow.png", false);
url.WriteToPasteboard(pasteboard);
}
}
Note: That is just one way to transfer a file, there are three other file drag transfers, you can also provide an array of urls, a promise to create the file yourself later and actually inserting the file's data into the pasteboard.

Window Phone 8.1: Confusing about file location and writing data

I'm trying to read and write data with json file.
I created some class.
public class SimpleTask{...}
public class DayTask{...}
public class DataModel
{
...
private async Task GetSimpleTaskAsync()
{
if (_daytask.Count != 0)
return;
string fileName = "a.json";
Uri appUri = new Uri("ms-appx:///"+ fileName);
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(appUri);
string jsonText = await FileIO.ReadTextAsync(file);
JsonObject jsonObject = JsonObject.Parse(jsonText);
JsonArray jsonArray = jsonObject["DayTasks"].GetArray();
foreach (JsonValue daytaskValue in jsonArray)
{
JsonObject daytaskObject = daytaskValue.GetObject();
ObservableCollection<SimpleTask> simpletask = new ObservableCollection<SimpleTask>();
foreach (JsonValue simpletaskValue in daytaskObject["Tasks"].GetArray())
{
JsonObject simpletaskObject = simpletaskValue.GetObject();
simpletask.Add(new SimpleTask( simpletaskObject["StartTime"].GetString(),
simpletaskObject["EndTime"].GetString(),
simpletaskObject["Description"].GetString()));
}
DayTask daytask = new DayTask(daytaskObject["Day"].GetString(),simpletask);
this.DayTasks.Add(daytask);
}
}
}
As you can see, i have a method that gets data form a.json file. I created a.json file:
In the MainPage.xaml.cs, there is a method which calls GetDayTaskAysnc() method and retrieves data :
private async void ReadData1(object sender, TappedRoutedEventArgs e)
{
string test = String.Empty;
var daytask = await DataModel.GetDayTaskAsync();
foreach (var tasks in daytask)
{
test += String.Format("Day:{0}:\n", tasks.Day);
foreach (var simpletask in tasks.Tasks)
{
test += String.Format("\tStart Time: {0}\n", simpletask.StartTime);
test += String.Format("\tEnd Time: {0}\n", simpletask.EndTime);
test += String.Format("\tDescription Time: {0}\n", simpletask.Description);
}
}
TextBlock.Text = test;
}
It worked fine ! But i want to write data to the same file, so i added data in hardcore way:
private List<DayTask> creatList()
{
List<DayTask> DayTasks = new List<DayTask>();
ObservableCollection<SimpleTask> simpletask1 = new ObservableCollection<SimpleTask>();
simpletask1.Add(new SimpleTask("6AM","7AM","Breakfast"));
simpletask1.Add(new SimpleTask("8AM", "9AM", "Game"));
ObservableCollection<SimpleTask> simpletask2 = new ObservableCollection<SimpleTask>();
simpletask2.Add(new SimpleTask("6AM", "7AM", "Sleep"));
simpletask2.Add(new SimpleTask("8AM", "9AM", "School"));
DayTasks.Add(new DayTask ("3/8/2014",simpletask1));
DayTasks.Add(new DayTask("4/8/2014", simpletask2));
return DayTasks;
}
private async void WriteData(object sender, TappedRoutedEventArgs e)
{
string json = "a.json";
List<DayTask> daytasks = creatList();
var serializer = new DataContractJsonSerializer(typeof(List<DayTask>));
var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(json, CreationCollisionOption.ReplaceExisting);
using (stream)
{
serializer.WriteObject(stream, daytasks);
}
TextBlock.Text = "Write to Json file succeeded";
}
When i ran my app with window phone emulator, Firstly, it wrote to the file. Then i clicked read data button to ensure data written correctly, the emulator showed data from a.json file without being modified by WriteData() method. I continued to creat the second read data method:
private async void ReadData2(object sender, TappedRoutedEventArgs e)
{
string test = String.Empty;
string json = "a.json";
string content = String.Empty;
List<DayTask> mytasks = new List<DayTask>();
var deserializer = new DataContractJsonSerializer(typeof(List<DayTask>));
var stream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(json);
using (stream)
{
mytasks = (List<DayTask>)deserializer.ReadObject(stream);
}
foreach (var tasks in mytasks)
{
test += String.Format("Day:{0}:\n", tasks.Day);
foreach (var simpletask in tasks.Tasks)
{
test += String.Format("\tStart Time: {0}\n", simpletask.StartTime);
test += String.Format("\tEnd Time: {0}\n", simpletask.EndTime);
test += String.Format("\tDescription Time: {0}\n", simpletask.Description);
}
}
TextBlock.Text = test;
}
I deployed my app several times, and this is my result:
ReadData2() : 'System.IO.FileNotFoundException'
WriteData() -> ReadData1(): Data from a.json was shown
WriteData() -> ReadData2(): Data from creatList() was shown
WriteData() -> ReadData1(): Data from a.json was shown -> ReadData2(): Data from creatList() was shown
So that, i have some question:
Do i have 2 json files, one i created by adding into my project and the other one i created when ran WriteData() method ? What is their paths ?
If my file is data.json in DataSource folder, how can i write data to it ? I can read data from it using uri like GetSimpleTaskAsync() but i don't know how to write to it correctly. (I tried to convert object into string to write but can't read it again, i guess i wrote it in wrong way)
Sorry for my long post and my bad english :) Thank you very much
But i want to write data to the same file, so i added data in hardcore way:
Your are making confusion between ms-appx:/// and ms-appdata:/// folders (or ApplicationData.Current.LocalFolder )
The ms-appx folder is read-only. You can't write to it. (or you could edit your app code without passing through the certification process)
The file you wrote must be into the ms-appdata folder.

C# Winforms Shorter Way to Add Items into ComboBox

May I know is there a shorter way to add items to the comboBox? Currently I am only adding 20 items which already seems very long, what if I have a 100 items to add into the comboBox?
My code:
private void loadSharePricesComboBox()
{
comboComSymbol.Items.Add("BARC");
comboComSymbol.Items.Add("DEB");
comboComSymbol.Items.Add("DOM");
comboComSymbol.Items.Add("EZJ");
comboComSymbol.Items.Add("GFS");
comboComSymbol.Items.Add("IHG");
comboComSymbol.Items.Add("JD.");
comboComSymbol.Items.Add("LAD");
comboComSymbol.Items.Add("LLOY");
comboComSymbol.Items.Add("MRW");
comboComSymbol.Items.Add("NXT");
comboComSymbol.Items.Add("OCDO");
comboComSymbol.Items.Add("RBS");
comboComSymbol.Items.Add("SMWH");
comboComSymbol.Items.Add("SPD");
comboComSymbol.Items.Add("STAN");
comboComSymbol.Items.Add("SYR");
comboComSymbol.Items.Add("TALK");
comboComSymbol.Items.Add("TSCO");
comboComSymbol.Items.Add("WMH");
comboComSymbol.SelectedIndex = -1;
}
Your help is much appreciated! Thank you. :)
Addition code (for the question i asked Simon Whitehead):
private void btnDownloadXML_Click(object sender, EventArgs e)
{
using (WebClient client = new WebClient())
{
client.DownloadFile("http://www.lse.co.uk/chat/" + comboDownloadXML.SelectedItem,
#"..\..\sharePriceXML\" + comboDownloadXML.SelectedItem + ".xml");
}
MessageBox.Show("Download Completed! File has been placed in the folder sharePriceXML!");
}
Have you tried the AddRange() method?
I haven't tested:
private void loadSharePricesComboBox()
{
comboComSymbol.Items.AddRange(new string[]{"BARC", "DEB", ... etc});
comboComSymbol.SelectedIndex = -1;
}
The MSDN on .AddRange might give you a better idea.
foreach(var item in "BARC,DEB,DOM,...".Split(',')) comboComSybol.Items.Add(item);
or
var items = new [] { "BARC", "DEV", "DOM" };
foreach(var item in items) comboComSymbol.Items.Add(item);
or you can save even more code and use AddRange on the above 2 methods.
var items = new [] { "BARC", "DEV", "DOM" };
comboComSymbol.Items.AddRange(items);
If you are starting a new project though, have a look at WPF instead of winforms.
Use ListBox.ObjectCollection.AddRangeYou can use it like this:comboComSymbol.Items.AddRange(new string[] {"ABC", "DEF", "GHI"});
To save on code size.. why not list them in a file?
void loadSharePricesComboBox(string fileName) {
using (StreamReader sr = new StreamReader(fileName)) {
while (!sr.EndOfStream) {
comboComSymbol.Items.Add(sr.ReadLine());
}
}
}
EDIT: In response to your comment.. I would just load the files, without extensions.. that would be much easier:
void loadSharePricesComboBox(string path) {
foreach (string file in Directory.GetFiles(path, "*.xml")) {
comboComSymbol.Items.Add(Path.GetFileNameWithoutExtension(file));
}
}
Pass in the path you want to load the XML file names from, perhaps like this:
loadSharePricesComboBox(#"..\..\sharePriceXML\");
This will load all the XML file names, without their extensions, giving you the list you require.
use generic List<T> to databind.
class Symbols
{
public string Name{get;set;}
}
var Symb= new List<Symbols> { new Symbols() { Name = "Abc"}, new Person() { Name = "BC" }};
comboBox1.DisplayMember = "Name";
comboBox1.DataSource = Symb;
comboBox1.DataBindings.Add("SelectedItem", Symb, "Name");
this code :
string[] str = {
"BARC","DEB","DOM","EZJ","GFS","IHG","JD.","LAD","LLOY","MRW",
"NXT","OCDO","RBS","SMWH","SPD","STAN","SYR","TALK","TSCO","WMH"
};
loadSharePricesComboBox(str);
your method :
private void loadSharePricesComboBox(string[] strArr)
{
comboComSymbol.Items.AddRange(strArr);
comboComSymbol.SelectedIndex = -1;
}

Categories

Resources