2014-09-11 4 views
4

web.config의 간단한 구성 스위치를 사용하여 모바일 버전과 응용 프로그램의 데스크톱 버전을 전환 할 수 있습니다. 그것은 bool 플래그 thats입니다. 그런 다음 Global.asax 파일에 다음 코드를 :모바일보기가 존재하지 않는 경우 데스크톱보기로 대체하는 방법

private void ConfigureMobileViewSwitcher() 
     { 
      if(ConfigurationManager.AppSettings.Get("Mobile") != null) 
      { 
       bool isMobile = Convert.ToBoolean(ConfigurationManager.AppSettings["Mobile"]); 

       // only inject the mobile display mode if the switch is set in app settings 
       if (isMobile) 
       { 
        DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("Mobile") 
        { 
         ContextCondition = (context => isMobile) 
        }); 
       } 
      } 
     } 

문제 모바일 스위치에 해당하는 경우 방법은 다음 존재하지 않는 .Mobile 페이지와 모바일 페이지를 사용하는 것이이 오류가 발생하는 것입니다 동일한보기의 데스크톱 버전으로 전환하는 대신 SIMPLE 영어

The following sections have been defined but have not been rendered for the layout page 

설명 :

기본적으로 나는 모바일보기가 특정 작업을 위해 존재하지 않는 경우 다음 그 뷰의 데스크톱 버전을 렌더링하는 것입니다 원하는.

UPDATE :

은 내가 DisplayModeProvider.Instance.Modes 수집을 확인하고 초기 출시에 2 개의 항목이 포함되어 있습니다.

Index 0 has "Mobile 
Index 1 has "" which I believe is the default 

Then I insert another "Mobile" on Index 0 

아래 스크린 샷은 자세한 내용을 보여줍니다. 내가 이동하고 페이지/뷰가 존재하지 않는에서 페이지를 보려고하면

The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/MainLayout.cshtml": "head; breadcrumb; banner; leftContent; mainContent" 

나는 위의 메시지가 :

enter image description here

여기에 실제 오류입니다. 그러나이 페이지는 데스크톱 애플리케이션 용으로 존재하며 모바일 앱은 데스크톱 뷰로 대체되어야합니다.

+0

디버그에서 실행중인 많은 모드는 제공자 인스턴스에 있습니까? – cheesemacfly

+0

원본 질문을 업데이트하여 자세한 내용을 추가했습니다. –

+0

지금은 테스트 할 수 없지만 올바르게 기억한다면 모바일보기를 사용할 수 없다면 다시 '보통'보기로 넘어갑니다. 새 디스플레이 모드를 추가하지 않고 작동하는지 확인할 수 있습니까? (그래서 당신은 단지 2 개의 디폴트 모드를 가져야한다) – cheesemacfly

답변

0

두 개의 디스플레이 모드가 구성되어 있으면이 방법이 효과적입니다. 첫째, 데스크톱 뷰 (Index.cshtml)의 표시를 시작하는 빈 접미사가있는 DefaultDisplayMode, 모바일 뷰가있는 데스크톱 뷰를 재정의하는 사용자 지정 모바일 표시 모드 (Index.Mobile .cshtml). 모바일보기가 존재하지 않으면 바탕 화면보기로 되돌아갑니다.

public class CustomMobileDisplayMode : DefaultDisplayMode 
{ 
    public CustomMobileDisplayMode() : base("Mobile") 
    { 
     ContextCondition = httpContext => IsCustomMobile(httpContext); 
    } 

    private bool IsCustomMobile(HttpContextBase httpContext) 
    { 
     // All mobile devices will trigger the Mobile view 
     // Or, if the override setting is in the config 
     return httpContext.Request.Browser.IsMobileDevice || 
       Convert.ToBoolean(ConfigurationManager.AppSettings["Mobile"]); 
    } 
} 

어딘가에는, 구성 중, 두 디스플레이 모드를 초기화하고 올바른 순서로 추가합니다 다음과 같은 것을 실행합니다.

DisplayModeProvider.Instance.Modes.Clear(); 
DisplayModeProvider.Instance.Modes.Add(new DefaultDisplayMode()); 
DisplayModeProvider.Instance.Modes.Add(new CustomMobileDisplayMode()); 
관련 문제