Getting an array of bytes out of Windows::Storage::Streams::IBuffer

You can use IBufferByteAccess, through exotic COM casts: byte* GetPointerToPixelData(IBuffer^ buffer) { // Cast to Object^, then to its underlying IInspectable interface. Object^ obj = buffer; ComPtr<IInspectable> insp(reinterpret_cast<IInspectable*>(obj)); // Query the IBufferByteAccess interface. ComPtr<IBufferByteAccess> bufferByteAccess; ThrowIfFailed(insp.As(&bufferByteAccess)); // Retrieve the buffer data. byte* pixels = nullptr; ThrowIfFailed(bufferByteAccess->Buffer(&pixels)); return pixels; } Code sample copied from http://cm-bloggers.blogspot.fi/2012/09/accessing-image-pixel-data-in-ccx.html

Update UI from thread in WinRT

The preferred way to deal with this in WinRT (and C# 5 in general) is to use async–await: private async void Button_Click(object sender, RoutedEventArgs e) { string text = await Task.Run(() => Compute()); this.TextBlock.Text = text; } Here, the Compute() method will run on a background thread and when it finishes, the rest of the … Read more

How write a file using StreamWriter in Windows 8?

Instead of StreamWriter you would use something like this: StorageFolder folder = ApplicationData.Current.LocalFolder; StorageFile file = await folder.CreateFileAsync(); using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) { using (IOutputStream outputStream = fileStream.GetOutputStreamAt(0)) { using (DataWriter dataWriter = new DataWriter(outputStream)) { //TODO: Replace “Bytes” with the type you want to write. dataWriter.WriteBytes(bytes); await dataWriter.StoreAsync(); dataWriter.DetachStream(); } await outputStream.FlushAsync(); … Read more

How to convert Platform::String to char*?

Here is a very simple way to do this in code w/o having to worry about buffer lengths. Only use this solution if you are certain you are dealing with ASCII: Platform::String^ fooRT = “aoeu”; std::wstring fooW(fooRT->Begin()); std::string fooA(fooW.begin(), fooW.end()); const char* charStr = fooA.c_str(); Keep in mind that in this example, the char* is … Read more

How to properly read and write a file using Windows.Storage on Windows Phone 8

Here’s a simple example: public async Task WriteDataToFileAsync(string fileName, string content) { byte[] data = Encoding.Unicode.GetBytes(content); var folder = ApplicationData.Current.LocalFolder; var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); using (var s = await file.OpenStreamForWriteAsync()) { await s.WriteAsync(data, 0, data.Length); } } public async Task<string> ReadFileContentsAsync(string fileName) { var folder = ApplicationData.Current.LocalFolder; try { var file = await … Read more

Windows 8 ListView with horizontal item flow

You can use a ListView this way: <ListView Height=”500″ VerticalAlignment=”Center” ScrollViewer.HorizontalScrollBarVisibility=”Auto” ScrollViewer.VerticalScrollBarVisibility=”Disabled” ScrollViewer.HorizontalScrollMode=”Enabled” ScrollViewer.VerticalScrollMode=”Disabled” ScrollViewer.ZoomMode=”Disabled” SelectionMode=”None”> <ListView.ItemsPanel> <ItemsPanelTemplate> <ItemsStackPanel Orientation=”Horizontal” /> </ItemsPanelTemplate> </ListView.ItemsPanel> — that gives it a horizontal panel and the right ScrollBars for horizontal scrolling. Both ListView and GridView can cause problems when you get larger items. I think by default the items … Read more

RunAsync – How do I await the completion of work on the UI thread?

I found the following suggestion on a Microsoft github repository: How to await a UI task sent from a background thread. Setup Define this extension method for the CoreDispatcher: using System; using System.Threading.Tasks; using Windows.UI.Core; public static class DispatcherTaskExtensions { public static async Task<T> RunTaskAsync<T>(this CoreDispatcher dispatcher, Func<Task<T>> func, CoreDispatcherPriority priority = CoreDispatcherPriority.Normal) { var … Read more

Read text file in project folder in Windows Phone 8.1 Runtime

If you want to read a file from your project you can for example do it like this: string fileContent; StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@”ms-appx:///example.txt”)); using (StreamReader sRead = new StreamReader(await file.OpenStreamForReadAsync())) fileContent = await sRead.ReadToEndAsync(); Also please ensure that you have set the Build Action of your file as Content (it should be … Read more

How can I run multiple commands in just one cmd windows in Java?

With && you can execute more than one commands, one after another: Runtime.getRuntime().exec(“cmd /c \”start somefile.bat && start other.bat && cd C:\\test && test.exe\””); Using multiple commands and conditional processing symbols You can run multiple commands from a single command line or script using conditional processing symbols. When you run multiple commands with conditional processing … Read more