【发布时间】:2015-08-26 00:13:12
【问题描述】:
我已经看到了很多关于此的问题,但没有关于如何在使用 DefaultDisplayMode 类确定移动站点的 Razor/MVC 站点中执行此操作的明确答案。另外,很多答案只是代码 sn-ps,没有详细说明代码的去向(是在控制器、视图、CSS 等中吗?)
首先,有一个全局文件调用 EvaluateDisplayMode():
Global.asax.cs
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
BundleConfig.RegisterBundles(BundleTable.Bundles);
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
DeviceConfig.EvaluateDisplayMode();
}
}
App_Start 文件夹中的 EvaluateDisplayMode 基于 GetOverriddenUserAgent() 类型设置移动和桌面类:
DeviceConfig.cs
public static class DeviceConfig
{
const string DeviceTypePhone = "Mobile";
public static void EvaluateDisplayMode()
{
DisplayModeProvider.Instance.Modes.Insert(0,
new DefaultDisplayMode(DeviceTypePhone)
{
ContextCondition = (ctx => (
(ctx.GetOverriddenUserAgent() != null) &&
(
(ctx.GetOverriddenUserAgent().IndexOf("android", StringComparison.OrdinalIgnoreCase) >= 0) ||
(ctx.GetOverriddenUserAgent().IndexOf("iPhone", StringComparison.OrdinalIgnoreCase) >= 0) ||
(ctx.GetOverriddenUserAgent().IndexOf("Window Phone", StringComparison.OrdinalIgnoreCase) >= 0) ||
(ctx.GetOverriddenUserAgent().IndexOf("Blackberry", StringComparison.OrdinalIgnoreCase) >= 0)
)
))
});
}
}
每个页面都有一个名为 _AdminMaster.Mobile.cshtml 的布局/母版页面,该页面使用名为 PhoneCommon.css 的 CSS 文件。 CSS 没有什么特别之处(移动与桌面;即没有媒体查询;我没有正确的 CSS,我从客户端使用的另一个开发人员那里继承了它)除了高度没有一直延伸到底部页。这是 CSS 的一部分(很长):
#main #content {
float:left;
display:block;
width:100%;
margin:0 auto;
position: relative;
top:7px;
left:0;
border:1px solid #000;
box-shadow:0 0 0 1px #464646;
-webkit-box-shadow:0 0 0 1px #464646;
-moz-box-shadow:0 0 0 1px #464646;
background:url(../images/login_bg.png) repeat top center;
border-radius:5px;
-webkit-border-radius:5px;
-moz-border-radius:5px;
position:relative;
min-height:300px; padding-bottom:30px;
}
最好的答案似乎来自@uʍopǝpısdn 这里:
,但它不起作用。我将旋转代码放在 PhoneCommon.css 中。它的作用是强制页面进入左上角并且不旋转它。
以下是我查看的其他一些网站,但没有一个显示完整的答案。有人可以告诉我如何让我的网站强制在移动设备上横向显示吗?谢谢。
Foundation: Force landscape mode on mobile devices
Force tablet to be in landscape mode
Is it possible to prevent iPhone/iPad orientation changing in the browser?
http://www.w3.org/TR/2004/CR-css-print-20040225/#section-properties
Is there a way to force horizontal / landscape layout on mobile devices?
【问题讨论】:
标签: css asp.net-mvc mobile responsive-design media-queries