我有一个WebSharper网站,我希望它在移动设备上拥有不同的用户界面。我希望服务器使用不同的HTML进行响应,具体取决于用户是否在移动设备上。我可以看看如何检查用户是否在mobile device in ASP.NET,但我无法告诉他如何将其移植到WebSharper。
答案 0 :(得分:3)
WebSharper中的Context
对象有一个Environment
字典,其中包含原始上下文。您可以通过该Request.Browser.IsMobileDevice
到达module Site
open WebSharper
open WebSharper.Sitelets
open WebSharper.UI.Next
open WebSharper.UI.Next.Server
type EndPoint =
| [<EndPoint "/">] Home
open WebSharper.UI.Next.Html
type Page = { Desktop: Doc; Mobile: Doc }
let mkPage desktop mobile = { Desktop = desktop; Mobile = mobile }
let HomePage =
mkPage
<| h1 [text "Desktop page!"]
<| h1 [text "Mobile page!"]
let PickPage (ctx : Context<_>) page =
let context = ctx.Environment.["HttpContext"] :?> System.Web.HttpContextWrapper
if context.Request.Browser.IsMobileDevice then page.Mobile
else page.Desktop
|> Content.Page
[<Website>]
let Main =
Application.MultiPage (fun ctx endpoint ->
match endpoint with
| EndPoint.Home -> PickPage ctx HomePage
)
。使用UI.Next客户端 - 服务器模板,你可以做这样的事情:
Airport = randi(2,286,720)==1; %// airport grid
People = randi(1e3,286,720); %// people grid
PeopleOnAirport = People(Airport); %// logical mask
MaxPeople = max(PeopleOnAirport(:)); %// find maximum
[Pixel(:,1),Pixel(:,2),~] = find(People==MaxPeople); %// finds the location of the maximum.