한 프로젝트에서 Silverlight 및 WPF를 컴파일하기위한 모범 사례는 무엇입니까?
-
03-07-2019 - |
문제
방금 Silverlight 프로젝트를 완료했는데 이제 약간 청소해야 할 때입니다. 핵심 파일을 가져 와서 메인 실버 라이트 앱에서 참조 할 별도의 프로젝트에 넣고 싶습니다. 이 클래스 중 일부는 WPF와 호환되며 한 프로젝트에서 Silverlight / WPF 코드를 모두 가질 수 있기를 바랍니다. 내 이상적인 솔루션은 여러 구성을 허용하는 단일 프로젝트입니다. 그래서,
구성 : Silverlight가 생성됩니다 : Company.controls.silverlight.dll
구성 : WPF는 생성됩니다 : Company.controls.wpf.dll
정의를 통해 분리 된 동일한 파일에 동일한 소스를 가질 수 있습니까?
전에 이것을 한 사람이 있습니까?
편집하다: MyCompany.windows.controls와 같은 프로젝트 당 솔루션을 만들었으며 여기에는 MyCompany.windows.controls & myCompany.windows.controls.silverlight가 포함되어 있습니다. 이 2 개의 폴더와 함께 두 프로젝트에서 사용하는 파일이 포함 된 "공유 된"폴더가 있습니다. 지금까지 잘 작동합니다 :)
해결책
나는 그것을 직접 시도하지 않았지만 (여전히 Silverlight와 함께 플레이 할 시간을 찾으려고 노력하고 있음) 두 개의 프로젝트를 가진 하나의 솔루션, 하나는 Silverlight를 대상으로하고 다른 하나는 .net 3.5를 대상으로하고 공통 클래스 파일을 추가 할 수 없었습니다. 각 프로젝트는 링크로 (프로젝트를 마우스 오른쪽 버튼으로 클릭하고, 기존 항목 추가 ..., 링크로 추가)?
** 업데이트 : 프로젝트 링커와 관련하여 아래 Mark의 답변을 참조하십시오. 나는 Prism 2.0 Cal과 함께 다중 표적화 된 복합 응용 프로그램에서 이것을 사용해 왔으며 그것은 아름다운 것입니다. 나는 이것이 프리즘 1.0에 존재했다고 생각하지 않습니까?
다른 팁
업데이트 : 거의 항상 더 쉬운 방법이 있음을 보여줍니다. :-)
첫 번째 단계는 조건부 편집을 사용하여 Silverlight 특정 코드를 분리하는 것입니다. (나는 당신의 "기본"대상이 wpf입니다.)
둘째, 각 플랫폼의 코드를 컴파일하여 적절한 정의 및 어셈블리 참조를 설정하는 빌드 스크립트가 필요합니다.
오픈 소스를 살펴보십시오 Caliburn 프로젝트. 이 모든 일을합니다.
다음은 Caliburn의 ExtensionMethods 클래스의 예입니다.
public static T GetResource<T>(this FrameworkElement element, object key)
{
DependencyObject currentElement = element;
while (currentElement != null)
{
var frameworkElement = currentElement as FrameworkElement;
if (frameworkElement != null && frameworkElement.Resources.Contains(key))
return (T)frameworkElement.Resources[key];
#if !SILVERLIGHT
currentElement = (LogicalTreeHelper.GetParent(currentElement) ??
VisualTreeHelper.GetParent(currentElement));
#else
currentElement = VisualTreeHelper.GetParent(currentElement);
#endif
}
if (Application.Current.Resources.Contains(key))
return (T)Application.Current.Resources[key];
return default(T);
}
VS에서 Caliburn을 열고 컴파일하면 표준 프레임 워크를 준수합니다. 참조는 Silverlight가 아닌 .NET 3.5 및 WPF 용입니다. 또한 사전 처리 지시문이 "! Silverlight"인 이유이기도합니다.
빌드 스크립트 (Caliburn 사용 Nant)에는 각 플랫폼의 정의를 설정하는 대상이 있습니다 (예 : Caliburn의 Silverlight Target은 다음과 같습니다.
<target name="config-platform-silverlight20">
<property name="nant.settings.currentframework" value="silverlight-2.0"/>
<property name="build.platform" value="silverlight-2.0"/>
<property name="build.defines" value="${global.build.defines},SILVERLIGHT,SILVERLIGHT_20,NO_WEB,NO_REMOTING,NO_CONVERT,NO_PARTIAL_TRUST,NO_EXCEPTION_SERIALIZATION,NO_SKIP_VISIBILITY,NO_DEBUG_SYMBOLS"/>
<property name="current.path.bin" value="${path.bin}/silverlight-2.0/${build.config}"/>
<property name="current.path.test" value="${path.bin}/silverlight-2.0/tests" />
<property name="current.path.lib" value="${path.lib}/Silverlight" />
</target>
그런 다음 실제 Silverlight 빌드를 호출하는 대상이 있습니다.
<target name="platform-silverlight20" depends="config">
<if test="${framework::exists('silverlight-2.0')}">
<echo message="Building Caliburn ${build.version} for Silverlight v2.0."/>
<call target="config-platform-silverlight20"/>
<copy todir="${current.path.bin}">
<fileset basedir="${current.path.lib}">
<include name="*.dll"/>
<include name="*.xml"/>
</fileset>
</copy>
<call target="core"/>
<call target="messaging"/>
<call target="actions"/>
<call target="commands"/>
<call target="package-platform"/>
</if>
<if test="${not(framework::exists('silverlight-2.0'))}">
<echo message="Silverlight v2.0 is not available. Skipping platform."/>
</if>
</target>
마지막으로, Caliburn.core.dll을 생산하는 "Core"대상의 예입니다.
<target name="core" depends="config, ensure-platform-selected">
<mkdir dir="${current.path.bin}"/>
<csc keyfile="${path.src}/Caliburn.snk" noconfig="true" warnaserror="false" target="library" debug="${build.debug}" optimize="${build.optimize}" define="${build.defines}"
output="${current.path.bin}/Caliburn.Core.dll"
doc="${current.path.bin}/Caliburn.Core.xml">
<sources basedir="${path.src}">
<include name="${build.asminfo}"/>
<include name="Caliburn.Core/**/*.cs"/>
</sources>
<references basedir="${current.path.bin}">
<include name="mscorlib.dll"/>
<include name="System.dll"/>
<include name="System.Core.dll"/>
<!--WPF-->
<include name="PresentationCore.dll"/>
<include name="PresentationFramework.dll"/>
<include name="WindowsBase.dll"/>
<!--Silverlight-->
<include name="System.Windows.dll" />
</references>
<nowarn>
<warning number="1584"/>
</nowarn>
</csc>
</target>
필요한 어셈블리를 참조하는 방법에 주목하십시오.
올바른 버전의 Silverlight 프레임 워크와 일치하려면 nant.exe.config (nant를 사용하는 경우)를 편집해야 할 것입니다. Silverlight RTW의 경우 프레임 워크 버전은 2.0.31005.0입니다.
"Patterns & Practices : Composite WPF 및 Silverlight"를 확인해야합니다.
http://www.codeplex.com/compositewpf/wiki/view.aspx?title=home
하나의 솔루션에서 동일한 앱의 WPF/Silvelight 버전으로 빠르게 시작됩니다. 또한 링크를 사용하여 Silverlight 코드를 변경할 때 WPF 앱의 소스를 업데이트하는 "프로젝트 링커". 버전 특정 코드가 있으면 재정의 할 수 있습니다.
예제는 여전히 가장자리 주위에서 약간 거칠지 만 프로젝트를 진행하는 방법에 대한 아이디어를 줄 수 있습니다.
HTH