Added support for WP8 and WP8.1. Tested in WP8.1 emulator.

This commit is contained in:
RoopeHakulinen 2015-01-02 17:04:45 +02:00 committed by eddyverbruggen@gmail.com
parent 080aa28f94
commit b088c1fc11
3 changed files with 147 additions and 0 deletions

View File

@ -52,4 +52,16 @@
</js-module>
</platform>
<platform name="wp8">
<config-file target="config.xml" parent="/*">
<feature name="YoikScreenOrientation">
<param name="wp-package" value="YoikScreenOrientation"/>
</feature>
</config-file>
<js-module src="www/screenorientation.wp8.js" name="screenorientation.wp8">
<merges target="cordova.plugins.screenorientation" />
</js-module>
<source-file src="src/wp/YoikScreenOrientation.cs" />
</platform>
</plugin>

View File

@ -0,0 +1,127 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.Net;
using System.IO;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Notification;
using Microsoft.Phone.Shell;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using WPCordovaClassLib.Cordova;
using WPCordovaClassLib.Cordova.Commands;
using WPCordovaClassLib.Cordova.JSON;
namespace WPCordovaClassLib.Cordova.Commands
{
public class YoikScreenOrientation : BaseCommand
{
#region Static members
private const string UNLOCKED = "unlocked";
private const string PORTRAIT = "portrait";
private const string LANDSCAPE = "landscape";
#endregion
/// <summary>
/// Current orientation
/// </summary>
private string currentOrientation;
public YoikScreenOrientation()
{
}
/// <summary>
/// Changes the orientation
/// </summary>
public void screenOrientation(string options)
{
string orientation = null;
try
{
orientation = JSON.JsonHelper.Deserialize<string[]>(options)[0];
}
catch (Exception ex)
{
this.DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, ex.Message));
return;
}
if (string.IsNullOrEmpty(orientation))
{
this.DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION));
return;
}
if (this.currentOrientation != orientation) // Might prevent flickering
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
PhoneApplicationFrame frame;
PhoneApplicationPage page;
if (TryCast(Application.Current.RootVisual, out frame) &&
TryCast(frame.Content, out page))
{
if (orientation == PORTRAIT)
{
page.SupportedOrientations = SupportedPageOrientation.Portrait;
}
else if (orientation == LANDSCAPE)
{
page.SupportedOrientations = SupportedPageOrientation.Landscape;
}
else if (orientation == UNLOCKED)
{
page.SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape;
}
else
{
this.DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Screen orientation not detected."));
return;
}
this.currentOrientation = orientation;
}
});
this.DispatchCommandResult();
}
}
static bool TryCast<T>(object obj, out T result) where T : class
{
result = obj as T;
return result != null;
}
}
}

View File

@ -0,0 +1,8 @@
var exec = require('cordova/exec'),
screenOrientation = {};
screenOrientation.setOrientation = function(orientation) {
exec(null, null, "YoikScreenOrientation", "screenOrientation", [orientation]);
};
module.exports = screenOrientation;