mirror of
https://github.com/chibicitiberiu/drumkit.git
synced 2024-02-24 10:53:32 +00:00
Latest build (version 2.2)
This commit is contained in:
125
DrumKit/Utils/IOHelper.cs
Normal file
125
DrumKit/Utils/IOHelper.cs
Normal file
@ -0,0 +1,125 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.IO;
|
||||
using Windows.Storage;
|
||||
using System.Xml.Serialization;
|
||||
|
||||
namespace DrumKit
|
||||
{
|
||||
static class IOHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a folder using relative path.
|
||||
/// </summary>
|
||||
public static async Task<StorageFolder> GetFolderRelativeAsync(StorageFolder root, string path)
|
||||
{
|
||||
// Split the path
|
||||
var splitpath = path.Split(new char[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
var currentdir = root;
|
||||
|
||||
// Browse to the last folder in the path
|
||||
for (int i = 0; i < splitpath.Length; i++)
|
||||
currentdir = await currentdir.GetFolderAsync(splitpath[i]);
|
||||
|
||||
// Return file
|
||||
return currentdir;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a file using relative path
|
||||
/// </summary>
|
||||
public static async Task<StorageFile> GetFileRelativeAsync(StorageFolder root, string path)
|
||||
{
|
||||
// Split the path
|
||||
var dir = await GetFolderRelativeAsync(root, System.IO.Path.GetDirectoryName(path));
|
||||
|
||||
// Return file
|
||||
return await dir.GetFileAsync(System.IO.Path.GetFileName(path));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a folder using relative path.
|
||||
/// </summary>
|
||||
public static async Task<StorageFolder> CreateFolderRelativeAsync(StorageFolder root, string path)
|
||||
{
|
||||
// Split the path
|
||||
var splitpath = path.Split(new char[] { '\\', '/' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
var currentdir = root;
|
||||
|
||||
// Browse to the last folder in the path
|
||||
for (int i = 0; i < splitpath.Length - 1; i++)
|
||||
currentdir = await currentdir.GetFolderAsync(splitpath[i]);
|
||||
|
||||
// Create folder
|
||||
return await currentdir.CreateFolderAsync(splitpath.Last(), CreationCollisionOption.ReplaceExisting);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a file using a relative path.
|
||||
/// </summary>
|
||||
public static async Task<StorageFile> CreateFileRelativeAsync(StorageFolder root, string path)
|
||||
{
|
||||
var currentdir = await GetFolderRelativeAsync(root, System.IO.Path.GetDirectoryName(path));
|
||||
|
||||
// Create file
|
||||
return await currentdir.CreateFileAsync(System.IO.Path.GetFileName(path), CreationCollisionOption.ReplaceExisting);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads an image, and returns an ImageSource (using relative path)
|
||||
/// </summary>
|
||||
public static async Task<Windows.UI.Xaml.Media.ImageSource> GetImageAsync(StorageFolder root, string path)
|
||||
{
|
||||
// Open file
|
||||
var file = await GetFileRelativeAsync(root, path);
|
||||
var stream = await file.OpenReadAsync();
|
||||
|
||||
// Get image
|
||||
var image = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
|
||||
await image.SetSourceAsync(stream);
|
||||
|
||||
// Return result
|
||||
return image;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserializes a storage file.
|
||||
/// </summary>
|
||||
public static async Task<object> DeserializeFile(StorageFile file, Type type)
|
||||
{
|
||||
// Open manifest file
|
||||
var stream = await file.OpenReadAsync();
|
||||
var iostream = stream.AsStream();
|
||||
|
||||
// Deserialize
|
||||
XmlSerializer serializer = new XmlSerializer(type);
|
||||
object res = serializer.Deserialize(iostream);
|
||||
|
||||
// Cleanup
|
||||
iostream.Dispose();
|
||||
|
||||
// Return result
|
||||
return res;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializes to a storage file.
|
||||
/// </summary>
|
||||
public static async Task SerializeFile(StorageFile file, object obj, Type type)
|
||||
{
|
||||
// Open manifest file
|
||||
var stream = await file.OpenAsync(FileAccessMode.ReadWrite);
|
||||
var iostream = stream.AsStream();
|
||||
|
||||
// Deserialize
|
||||
XmlSerializer serializer = new XmlSerializer(type);
|
||||
serializer.Serialize(iostream, obj);
|
||||
|
||||
// Cleanup
|
||||
iostream.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
55
DrumKit/Utils/UIHelper.cs
Normal file
55
DrumKit/Utils/UIHelper.cs
Normal file
@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.UI.ViewManagement;
|
||||
using Windows.UI.Xaml;
|
||||
using Windows.UI.Xaml.Media;
|
||||
|
||||
namespace DrumKit
|
||||
{
|
||||
static class UIHelper
|
||||
{
|
||||
public static DrumkitLayoutTargetView ToDrumkitLayoutView(ApplicationViewState state)
|
||||
{
|
||||
switch (state)
|
||||
{
|
||||
case ApplicationViewState.Filled:
|
||||
return DrumkitLayoutTargetView.Filled;
|
||||
|
||||
case ApplicationViewState.FullScreenLandscape:
|
||||
return DrumkitLayoutTargetView.Landscape;
|
||||
|
||||
case ApplicationViewState.FullScreenPortrait:
|
||||
return DrumkitLayoutTargetView.Portrait;
|
||||
|
||||
case ApplicationViewState.Snapped:
|
||||
return DrumkitLayoutTargetView.Snapped;
|
||||
}
|
||||
|
||||
return DrumkitLayoutTargetView.None;
|
||||
}
|
||||
|
||||
public static FrameworkElement FindChildByName(FrameworkElement el, string name)
|
||||
{
|
||||
if (el == null || string.IsNullOrEmpty(name))
|
||||
return null;
|
||||
|
||||
if (name == el.Name)
|
||||
return el;
|
||||
|
||||
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(el); i++)
|
||||
{
|
||||
var element = VisualTreeHelper.GetChild(el, i) as FrameworkElement;
|
||||
var result = FindChildByName(element, name);
|
||||
|
||||
if (result != null)
|
||||
return result;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user