Launch a file in WinRT using half screen - c#

Im trying to open a file in a Windows Store App , with half the screen but so far i havent got it to work
this is the code im using
try
{
var options = new Windows.System.LauncherOptions();
options.DesiredRemainingView = Windows.UI.ViewManagement.ViewSizePreference.UseHalf;
var urii = new Uri(file.Path);
var success = await Windows.System.Launcher.LaunchUriAsync(urii, options);
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
the file.path is
C:\Users\xxxx\AppData\Local\Packages\xxxxxxx\LocalState\Data\chap4_slides.ppt
and i can launch the file if i use
await Windows.System.Launcher.LaunchFileAsync(file, options);
but it doesnt uses half the screen
am i doing something wrong?

You're doing it right, but the DesiredRemainingView is explicitly a request not a command. As the remarks in the DesiredRemainingView docs say: By setting DesiredRemainingView, you aren't guaranteed a specific windowing behavior for the source app.
In your case I suspect you're not getting the desired view because your ppt file is launching in PowerPoint on the desktop.

Related

Replacing a file previously used for source in MediaElement in Windows Phone 8.1 throws E_ACCSESSDENIED

What I want to do is to let user record his voice. The recording would be stored in local storage. User should be able to play the recording or replace it with a new one. And that's where I am having trouble.
Scenario A: there is no recording yet, this works fine... when user presses record button, these steps are taken to record the audio and then set it as source for media element.
// user starts recording by pressing a button
// create file with replace option
var audioStorageFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.ReplaceIfExists);
await mediaCapture.StartRecordToStorageFileAsync(audioEncodingProperties, audioStorageFile);
// user stops recording...
await mediaCapture.StopRecordAsync();
//...
audioStream = await audioStorageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
MyMediaElement.SetSource(audioStream, audioStorageFile.ContentType);
I can repeat this proccess as many times as I want and the old audio file is always replaced with the new one.
Scenario B: Recording already exists when I navigate to the page so I want to load it to the MediaElement right away (OnNavigatedTo event) like this
var audioStorageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(desiredName);
audioStream = await audioStorageFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
MyMediaElement.SetSource(audioStream, audioStorageFile.ContentType);
So user navigates to the page and file is already loaded to the MediaElement. The problem starts when user wants to replace the old recording. This piece of code is called before the code from Scenario A:
MyMediaElement.Stop();
MyMediaElement.Source = null;
When it reaches the line
var audioStorageFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.ReplaceIfExists);
UnauthorizedAccessException is thrown: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
It seems clear that the reason for the exception being thrown is that the file I'm trying to replace is in use. But I don't understand why and how to avoid it? Can anyone spot what I'm doing wrong? Thanks.
Add .Source = null before you create the file
MyMediaElement.Source = null;
var audioStorageFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.ReplaceIfExists);
If you're not in the habit of closing/disposing your Streams after you are done...you going to have a bad time.
To make sure dispose gets called, get in the habit of using using
Set your MediaElement like so
var audioStorageFile = await ApplicationData.Current.LocalFolder.GetFileAsync("your_wav.wav");
using(Windows.Storage.Streams.IRandomAccessStream audio = await audioStorageFile.OpenAsync(FileAccessMode.Read))
{
this.myMed.SetSource(audio, audioStorageFile.ContentType);
}
Using the above methods, I was able to record over "your_wav.wav" no matter if it was already set as the Source of the MediaElement.
Ok, turns out that the problem actualy was that I was setting the source of the MediaElement before the MediaElement was added to the visual tree. I moved the code to set the media source from OnNavigatedTo to MediaElement.Loaded event and the problem was solved. I found the solution thanks to this question.

Cannot read JSON file - FileNotFoundException

I was following tutorial on this page http://channel9.msdn.com/Series/Windows-Phone-8-1-Development-for-Absolute-Beginners/Part-22-Storing-and-Retrieving-Serialized-Data because I want my app to store data in JSON file and then read it back. 2 things about the tutorial:
I press write button - works fine, then press read button and it also works, however, I close down win phone 8.1 emulator, open it again, press read button and I got the An exception of type 'System.IO.FileNotFoundException'exception! Why is that, I should have the file already on disk from previously running the app ? Or is does it get erased when I close down emulator ? Also I looked for the specified file on disk and cannot find it ! Any help ?
private const string JSONFILENAME = "data.json";
private async Task readJsonAsync()
{
string content = String.Empty;
var myStream = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(JSONFILENAME);
using (StreamReader reader = new StreamReader(myStream))
{
content = await reader.ReadToEndAsync();
}
resultTextBlock.Text = content;
}
private async Task writeJsonAsync()
{ // Notice that the write is ALMOST identical ... except for the serializer.
var myCars = buildObjectGraph();
var serializer = new DataContractJsonSerializer(typeof(List<Car>));
using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
JSONFILENAME,
CreationCollisionOption.ReplaceExisting))
{
serializer.WriteObject(stream, myCars);
}
resultTextBlock.Text = "Write succeeded";
}
When you close the Emulator its state is is not preserved, meaning that the apps that you tested are not there when you restart the emulator. Therefore, VS will make a new install when you open up the emulator again.
The same happens when you do a rebuild of your project. Then, VS will remove the app from the emulator and reinstall it from scratch. This in turn will also result in loosing your JSON file.
When you want to make sure that your data is preserved then don't close the emulator and just use Build from within VS (VS decides from time to time to rebuild your app though).
On order to test your app more properly I suggest you have a look at the Windows Phone Power Tools (http://wptools.codeplex.com/). There you can explicitly choose to install or update a given XAP package.

Document opening issue in Windows Phone 8.1

I am creating Windows Phone 8.1 application, I am trying to open document using launcher, but getting exception, and document is not MS OFFICE document, it is created in other software. Here is my code.
string file = "readme.txt";
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
if (isf.FileExists(file))
{
isf.DeleteFile(file);
}
var filerun = await ApplicationData.Current.LocalFolder.CreateFileAsync(file);
await Launcher.LaunchFileAsync(await ApplicationData.Current.LocalFolder.GetFileAsync(file));
I am getting error like this:
"Can't be Open, File Format doesn't recognize"
and sometimes like this:
"Document has been damaged"
I do not know how to deal with this, I am stuck here, Any help would be greatly appreciated.
The file name without extension is "campaign rev 2", so the file pass to the launcher is definitely not "readme.txt".
You can pass a LauncherOptions.DisplayApplicationPicker to the LaunchFileAsync method.
var filerun = await ApplicationData.Current.LocalFolder.CreateFileAsync(file);
var options = new Windows.System.LauncherOptions(){ DisplayApplicationPicker = true};
await Launcher.LaunchFileAsync(filerun, options);
It will open a list of applications for you to choose from, so you can examine the file extension is actually .txt or .xls/.xlsx (Excel).
According to the documentation, this overload is available on Windows Phone 8.1. But I have not upgraded to 8.1 yet, so I can not give you a screenshot of the Application Picker.
Screenshot from the web. Hope this helps.

Recording (or transforming after recording) video at non-standard size in Windows Phone 8

I am trying to capture MP4 video at a specific resolution in Windows Phone 8 (to be specific, 480x480). I know that I can't use sizes other than the presets, and 480x480 is not a preset. How do I transform a captured video (such as 640x480) and crop the top and bottom to make it 480x480? Any free or open source libraries (that run on Windows Phone) are welcome. Please don't answer with answers such as 'use an external server', I need an on-device solution.
Use the Windows.Phone.Media.Capture APIs and the AudioVideoCaptureDevice class
Second parameter for AudioVideoCaptureDevice.OpenAsync - see this link - is the resolution. And you can get the resolutions using AudioVideoCaptureDevice.GetAvailableCaptureResolutions(sensor)
EDIT: To set custom resolutions try AudioVideoCaptureDevice.SetCaptureResolutionAsync
EDIT 2: You could try something like the following to transform recorded video: (can't find where i got the code from soz to author!)
StorageFolder isoStore = await ApplicationData.Current.LocalFolder.GetFolderAsync("Shared");
var file = await isoStore.CreateFileAsync("foos1.wmv", CreationCollisionOption.ReplaceExisting);
using (var s = await file.OpenAsync(FileAccessMode.ReadWrite))
{
Windows.Foundation.Size resolution = new Windows.Foundation.Size(640, 480);
avDevice = await AudioVideoCaptureDevice.OpenAsync(CameraSensorLocation.Back,
AudioVideoCaptureDevice.GetAvailableCaptureResolutions(CameraSensorLocation.Back).Last());
VideoBrush videoRecorderBrush = new VideoBrush();
videoRecorderBrush.SetSource(avDevice);
viewfinderRectangle.Fill = videoRecorderBrush;
await avDevice.StartRecordingToStreamAsync(s);
Thread.Sleep(30000);
await avDevice.StopRecordingAsync();
}
new MediaPlayerLauncher()
{
Media = new Uri(file.Path, UriKind.Relative),
}.Show();

MonoTouch printing a local PDF file

I have a MonoTouch application that generates some PDFs locally and then prints them to a network printer. To get this working I initially just added a PDF resource to the project that I could try and print but am having a heck of a time. When I print just HMTL or a string value everything is good but printing the PDF has me lost. When debugging it looks like the app is getting the proper URL.
Any help would be greatly appreciated and my sampled code is below:
public void PrintSomePDF ()
{
var printInfo = UIPrintInfo.PrintInfo;
printInfo.OutputType = UIPrintInfoOutputType.General;
printInfo.JobName = "Test: PDF Print";
string pdfFileName = "printthispdf_01.pdf";
NSUrl url = NSUrl.FromFilename(pdfFileName);
var printer = UIPrintInteractionController.SharedPrintController;
printer.PrintInfo = printInfo;
printer.PrintingItem = url.Path;
printer.ShowsPageRange = true;
printer.Present (true, (handler, completed, err) => {
if (!completed && err != null){
Console.WriteLine ("error");
}
});
}
I was able to resolve the problem with the way I passed in the NSUrl to the PrintingItem. Before I was passing in printer.PrintingItem = url.Path; which was basically just passing in a string of the path and not the actual shape of NSUrl.
printer.PrintingItem = url;
What I've always preferred to do (and this depends very much on the deployed device - in my case it was a server I controlled) was to just install a PDF printer and then it's as easy as printing any other kind of document.
Something like BullZip is free and allows you to write any settings for the print to a RunOnce.ini (xml) file to have a silent mode print with settings for the filename and so forth.
Obviously not a great solution if you don't have control of the system you're deploying to, but a solid and easy one if you do.

Categories

Resources